编辑:解决了!见下文。
我是最近为python使用requests库的人。但是,我遇到了一种情况,即urllib2工作,而requests不工作。
import requests, urllib2
url = "http://thepiratebay.se/search/test/0/7/0"
print urllib2.urlopen(url).getcode() # 200 - OK!
print requests.get(url).status_code # 400 :(我也尝试过设置标题,但它似乎没有解决问题,所以我包含了我的最小示例。其他想法:urllib2是否以某种方式正确地处理重定向到thepiratebay.sx,而requests却没有?
提前感谢!
感谢大家的帮助,我升级了requests (1.0.4 => 1.2.2)并解决了问题。哎呀,早该考虑的!
发布于 2013-05-23 00:52:58
默认情况下,这两种方法都应该处理重定向。
在http://docs.python.org/2/library/urllib2.html中,urllib2处理“基本和摘要身份验证、重定向、cookie等”。
“从http://docs.python-requests.org/en/latest/user/quickstart.html#redirection-and-history”请求将自动执行位置重定向,同时使用GET和选项谓词。
这两个人都给了我200个回复。
https://stackoverflow.com/questions/16703916
复制相似问题