Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x urlopen返回有效链接的重定向错误_Python 3.x_Httprequest_Urllib - Fatal编程技术网

Python 3.x urlopen返回有效链接的重定向错误

Python 3.x urlopen返回有效链接的重定向错误,python-3.x,httprequest,urllib,Python 3.x,Httprequest,Urllib,我正在用python构建一个断开的链接检查器,而构建正确识别使用浏览器访问时无法解析的链接的逻辑正成为一件烦琐的事情。我发现了一组链接,在这些链接中,我可以用我的scraper一致地重现重定向错误,但当在浏览器中访问时,这些链接可以完美地解决问题。我希望我能在这里找到一些见解 import urllib import urllib.request import html.parser import requests from requests.exceptions import HTTPErro

我正在用python构建一个断开的链接检查器,而构建正确识别使用浏览器访问时无法解析的链接的逻辑正成为一件烦琐的事情。我发现了一组链接,在这些链接中,我可以用我的scraper一致地重现重定向错误,但当在浏览器中访问时,这些链接可以完美地解决问题。我希望我能在这里找到一些见解

import urllib
import urllib.request
import html.parser
import requests
from requests.exceptions import HTTPError
from socket import error as SocketError

try:
    req=urllib.request.Request(url, None, {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; G518Rco3Yp0uLV40Lcc9hAzC1BOROTJADjicLjOmlr4=) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36','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'})
    response = urllib.request.urlopen(req)
    raw_response = response.read().decode('utf8', errors='ignore')
    response.close()
except urllib.request.HTTPError as inst:
    output = format(inst)


print(output)
在此实例中,可靠返回此错误的URL示例为“”。它在访问时可以完美地解析,但上面的代码将返回以下错误:

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

你知道我怎样才能正确地识别这些链接的功能性,而不盲目地忽略来自该站点的链接(可能会错过真正断开的链接)?

你会得到无限循环错误,因为你想要刮取的页面使用cookie,当cookie不是由客户端发送时会重定向。当您不允许cookie时,大多数其他刮板工具和浏览器都会出现相同的错误

您需要一个
http.cookiejar.cookiejar
和一个
urllib.request.HTTPCookieProcessor
来避免重定向循环:

import urllib
import urllib.request
import html.parser
import requests
from requests.exceptions import HTTPError
from socket import error as SocketError
from http.cookiejar import CookieJar

try:
    req=urllib.request.Request(url, None, {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; G518Rco3Yp0uLV40Lcc9hAzC1BOROTJADjicLjOmlr4=) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36','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'})
    cj = CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    response = opener.open(req)
    raw_response = response.read().decode('utf8', errors='ignore')
    response.close()
except urllib.request.HTTPError as inst:
    output = format(inst)
    print(output)

我同意第一个答案中的评论,但它对我不起作用(我得到的是一些编码/压缩字节数据,没有可读性)

提到的链接使用urllib2。它还与python 3.7中的urllib配合使用,如下所示:

from urllib.request import build_opener, HTTPCookieProcessor
opener = build_opener(HTTPCookieProcessor())
response = opener.open('http://www.bad.org.uk')
print response.read()

我尝试了上述解决方案,但没有成功


当您试图打开的URL格式不正确(或者不是REST服务所期望的)时,可能会出现此问题。例如,我发现我的问题是因为我请求
https://host.com/users/4484486
主机希望在结尾处出现斜杠的位置:
https://host.com/users/4484486/
解决了问题。

出于某种原因,python2对我有效,但不是你的答案。我继续用这个方法得到一个302无限循环错误。有什么建议吗?当Python从版本2切换到版本3时,一些模块被重命名。您需要将
urllib
替换为
urllib2
http.cookiejar
替换为
cookielib
等。几分钟前,我用上述问题中提到的url对其进行了测试,它与Python 2.7一起工作可能还不清楚,但我的意思是我可以使用链接的
python2
方法打开url,但不是你的。这就是说,我认为我们可以忽略,我尝试的网站可能已经阻止了我,因为太多的拉入太少的时间(甚至不能在浏览器中访问它)。它今天又回来工作了。我想我被弄糊涂了。什么不起作用。谢谢如果我今天再试一次,我会使用请求库和适用于该工具的cookie函数:对于Python 3,如果我今天这样做,我可能会使用请求库中的工具: