Python: urllib2 没有得到任何确实存在的东西
Python: urllib2 get nothing which does exist
我正在尝试抓取我的大学网站并设置了 cookie,添加 headers 然后:
homepage=opener.open("website")
content = homepage.read()
print content
我有时能得到源代码,但有时却一无所获。
我不明白发生了什么。
我的代码有误吗?
或者网络很重要?
一个 geturl()
可以用来获得两倍甚至更多的重定向吗?
redirect = urllib2.urlopen(info_url)
redirect_url = redirect.geturl()
print redirect_url
它可以变成最后一个 url,但有时我会得到中间那个。
与其使用 urlopen 解决重定向问题,不如使用更强大的请求库:http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history
r = requests.get('website', allow_redirects=True)
print r.text
我正在尝试抓取我的大学网站并设置了 cookie,添加 headers 然后:
homepage=opener.open("website")
content = homepage.read()
print content
我有时能得到源代码,但有时却一无所获。
我不明白发生了什么。
我的代码有误吗?
或者网络很重要?
一个 geturl()
可以用来获得两倍甚至更多的重定向吗?
redirect = urllib2.urlopen(info_url)
redirect_url = redirect.geturl()
print redirect_url
它可以变成最后一个 url,但有时我会得到中间那个。
与其使用 urlopen 解决重定向问题,不如使用更强大的请求库:http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history
r = requests.get('website', allow_redirects=True)
print r.text