python HTTP错误302

时间:2018-06-03 00:07:57

标签: python html urllib

我一直得到一个html 302错误,但我很确定错误即将来临 从这里的一些代码我不知道究竟在哪里以及如何解决它。

headers ={'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3','Accept-Encoding': 'gzip, deflate, sdch','Accept-Language': 'en-US,en;q=0.8','Connection': 'keep-alive'}

req_obj = urllib.request.Request(url, method=method,headers=headers)

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
response = opener.open(req_obj)
json_resp = json.loads(response.read().decode('utf-8'))

我得到的错误是

HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found

(False, <HTTPError 302: 'The HTTP server returned a redirect error that would lead to an infinite loop.\nThe last 30x error message was:\nFound'>)

提前致谢

1 个答案:

答案 0 :(得分:0)

这是您尝试访问的服务器的问题。标头的Location参数与您尝试访问的网址相同,这会导致无限循环。

例如,如果我要转到http://example.com/foo,并告诉我资源已移至http://example.com/foo,我会按要求转到http://example.com/foo。但是,这会告诉我http://example.com/foo已移至http://example.com/foo。等等。