follow redirect using urllib2

594 views Asked by At

I am trying to follow the redirect of a url using urllib2.

>>> import urllib2
>>> page=urllib2.urlopen('http://acer.com')
>>> print page.geturl()
http://www.acer.com/worldwide/selection.html
>>>page=urllib2.urlopen('http://www.acer.com/worldwide/selection.html')
>>> print page.geturl()
http://www.acer.com/worldwide/selection.html

But when I open http://www.acer.com/worldwide/selection.html in my browser it redirects to http://us.acer.com/ac/en/US/content/home#_ga=1.216787925.232352975.1435019296 How to detect this redirection using urllib.

2

There are 2 answers

2
duFF On BEST ANSWER

get_url() doesn't work for all re-directs (for example JavaScript redirects)

What are you trying to achieve?

Something like Selenium with PhantomJS as the backend might be more suited to this.

For screenshots you can then use save_screenshot() which is part of Selenium Webdriver

0
LittleQ On

Use selenium to get start. I'm using chromedriver as browser:

from selenium.webdriver import Chrome cr = Chrome() cr.get(url) cr.save_screenshot('IMAGE_NAME.png')