- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 python 构建一个断开的链接检查器,它正在成为一项苦差事,构建正确识别使用浏览器访问时无法解析的链接的逻辑。我找到了一组链接,我可以在其中始终使用我的刮刀重现重定向错误,但在浏览器中访问时可以完美解决。我希望我能在这里找到一些见解。
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)
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)
关于python-3.x - urlopen 返回有效链接的重定向错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32569934/
我有一些代码使用 mechanize 和 beautifulsoup 来抓取一些数据。该代码在测试机器上运行良好,但生产机器正在阻止连接。我得到的错误是: urlopen error [Errno 1
我有一个正在测试的简单网站。它在本地主机上运行,我可以在我的网络浏览器中访问它。索引页就是简单的“运行”二字。 urllib.urlopen 将成功读取页面,但 urllib2.urlopen 不
我只是想更好地了解这里发生了什么,我当然可以使用 urllib2 来“解决”这个问题。 import urllib import urllib2 url = "http://www.crutchfie
import urllib print urllib.urlopen('http://www.reefgeek.com/equipment/Controllers_&_Monitors/Neptune
我刚刚开始编写 Python,并且遇到了 urllib 似乎是 Amazon.com 独有的问题。如果我做类似的事情: pageIn = urllib.request.urlopen("http://
我想模拟模块中对 urllib.request.urlopen 的调用。当它是单个文件时它可以工作,但是当我把它放在一个包中并在包的 __init__.py 中导入模块时,我不能再模拟它了。 复制 假
# Get the content type of a URL def get_url_type(url: str) -> str: r = urlopen(url) header = r.h
我尝试将现有 URL 作为参数传递,以将其 HTML 加载到单个 txt 文件中: for line in open('C:\Users\me\Desktop\URLS-HERE.txt'): if
我对使用 Request、urlopen 和 JSONDecoder().decode() 有点困惑。 目前我有: hdr = {'User-agent' : 'anything'} # heade
此代码用于打印网页。它现在打印一个空格,即使在浏览器中查看时该页面清楚地包含内容 from urllib.request import urlopen f = urlopen('http://onli
我刚买了 synology NAS (DS213J),我想在上面运行 python 脚本。 我的 python 脚本: 1 #!/opt/bin/python 2 3 import url
我在名为 urlopen_test() 的函数中使用 urllib.urlopen()。在这个函数中我调用了两次 urlopen,第一次调用很快,但第二次调用速度很快。有人打电话来帮我弄清楚为什么吗?
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
1.data参数 data是可选的,需要使用bytes()方法将参数转化为字节编码格式的内容。如果传递了这个参数,请求方式就不是GET方式,而是POST方式。
我正在使用 迭代保存在 http 网站上的 .dat 文件 import urllib2 test_file = urllib2.urlopen('http://~/file.dat') 然后,我有一
这是我的问题: import urllib2 response=urllib2.urlopen('http://proxy-heaven.blogspot.com/') html=response.r
我没有使用 python 的经验,也没有使用下面的代码打开 url 并读取响应的经验。我收到未经授权的错误,因为该网站使用 Windows 身份验证。有人可以提供有关如何发送用户名和密码的代码示例吗?
我希望我的程序尝试打开页面并在时间间隔[0,t]内获取数据,如果该时间到期,连接应该关闭。 我正在使用urllib2来尝试完成任务。 t=1 url="http://example.com" resp
我正在尝试使用 python 程序填写表格,它对于某些网站效果很好,但对于这个特定的网站则不然,我不知道为什么。 这是代码片段 query = { 'adults':'1', 'children':'
当我使用 urllib2.urlopen() 时,我在考虑它只是为了标题读取还是实际上带回了整个网页? IE 是否在调用 urlopen 或调用 read() 时实际获取 HTML 页面? handl
我是一名优秀的程序员,十分优秀!