我正在编写一个程序,我需要像在计算机上一样从一个页面跳转到另一个页面。使用urllib2从一个页面跳转到另一个页面
关于urllib我去了一个页面,并从该网页源获得一个网址,然后我采取该网址,我去了,但我从网站收到一个错误,说我来自错误的页面,因为urllib doesn' t从一页开始,转到另一页,它会打开一个新页面。 。 这里是一些示例代码:
tree = self.opener.open('http://www.example.com').read()
#Beautiful soup parsing to get the new _url
new_page = self.opener.open('http://www.example/new_url.com').read()
print new_page #Output is error page
我如何写它,以便该网站认为我从以前的页面我在哪里吗?
谢谢
您是否必须使用'urllib2' - 我觉得您会发现使用request和它的requests.session对象更容易使用。否则,您可能希望尝试设置引荐来源的标头,并查看是否使网站开心。 –
我正在考虑使用请求,但你可以添加一个标题请求,因为它不工作没有标题 – Serial
它就像'requests.get('http://example.com/page',headers = {'Referer' :'http://example.com'})' –