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?
You could override
HTTPRedirectHandler.redirect_request()method to save a redirection history:It should be much faster than the provided
WWW::Mechanizecode snippet becauseurllib2visits each url only once.mechanizeprovides a superset ofurllib2functionality i.e., if you usemechanizethen just replace every occurrence ofurllib2above withmechanizeand it will work.