Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7665739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:39:08+00:00 2026-05-31T14:39:08+00:00

I want to get the url redirection log using Mechanize written in Python. For

  • 0

I want to get the url redirection log using Mechanize written in Python. For example, http://www.google.com –> http://www.google.co.in. The exact question has been asked before in SO but it is for Ruby

How to get redirect log in Mechanize?

The answer explains that to do this one can do the following in Ruby –

for m.redirection_limit in 0..99
  begin
    m.get(url)
    break
    rescue WWW::Mechanize::RedirectLimitReachedError
      # code here could get control at
      # intermediate redirection levels
  end
end

I want to do the same using Python. Any help? What is the alternate of get(url) in Python for Mechanize?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:39:09+00:00Added an answer on May 31, 2026 at 2:39 pm

    You could override HTTPRedirectHandler.redirect_request() method to save a redirection history:

    import urllib2
    
    class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):
        def redirect_request(self, req, fp, code, msg, headers, newurl):
            newreq = urllib2.HTTPRedirectHandler.redirect_request(self,
                req, fp, code, msg, headers, newurl)
            if newreq is not None:
                self.redirections.append(newreq.get_full_url())
            return newreq
    
    url = 'http://google.com'
    
    h = HTTPRedirectHandler()
    h.max_redirections = 100
    h.redirections = [url]
    opener = urllib2.build_opener(h)
    response = opener.open(url)
    print h.redirections
    # -> ['http://google.com', 'http://www.google.com/', 'http://google.com.ua/']
    

    It should be much faster than the provided WWW::Mechanize code snippet because urllib2 visits each url only once.

    mechanize provides a superset of urllib2 functionality i.e., if you use mechanize then just replace every occurrence of urllib2 above with mechanize and it will work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to map a number of directories in a URL: www.example.com/manual www.example.com/login to
I want to get the Bitmap from URL like - https://graph.facebook.com/100003506521332/picture I tried how-i-can-display-images-from-url-in-blackberry
I want to get HTML using the HTTPWEBREQUEST for the following page: http://inkdispatch.com/brother Currently
I want to get a url from a string. Heres my code to extract
I'm just getting started with jQuery and I want to get the url of
Basically what the title says, I want to get the URL and HTTP Verb
I am creating a firefox extension. I want to get the URL of the
i am new to asp.net, ı want to get data from url on asp.net.
I want to get only the headers of an URL request. I have been
All I want is to get the website URL. Not the URL as taken

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.