使用urllib2跟随重定向

2024-05-16 02:28:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用urllib2跟踪url的重定向。在

>>> 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

但是当我在浏览器中打开http://www.acer.com/worldwide/selection.html时,它会重定向到http://us.acer.com/ac/en/US/content/home#_ga=1.216787925.232352975.1435019296如何使用urllib检测这种重定向。在


Tags: importcomhttpurlhtmlwwwpageurllib2
2条回答

get_url()并不适用于所有重定向(例如JavaScript重定向)

你想达到什么目的?在

PhantomJS为后端的Selenium可能更适合这种情况。在

对于屏幕截图,您可以使用^{},它是Selenium Webdriver的一部分

使用selenium开始。我使用chromedriver作为浏览器:

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

相关问题 更多 >