我一直在从特定网站获取 RSS 提要时遇到问题。我最终编写了一个相当丑陋的程序来执行此功能,但我很好奇为什么会发生这种情况以及是否有任何更高级别的接口(interface)正确处理此问题。这个问题并不是真正的阻碍,因为我不需要经常检索提要。
我已经阅读了一个捕获异常并返回部分内容的解决方案,但是由于不完整的读取在实际检索到的字节数上有所不同,我不确定这样的解决方案是否真的有效。
#!/usr/bin/env python
import os
import sys
import feedparser
from mechanize import Browser
import requests
import urllib2
from httplib import IncompleteRead
url = 'http://hattiesburg.legistar.com/Feed.ashx?M=Calendar&ID=543375&GUID=83d4a09c-6b40-4300-a04b-f88884048d49&Mode=2013&Title=City+of+Hattiesburg%2c+MS+-+Calendar+(2013)'
content = feedparser.parse(url)
if 'bozo_exception' in content:
print content['bozo_exception']
else:
print "Success!!"
sys.exit(0)
print "If you see this, please tell me what happened."
# try using mechanize
b = Browser()
r = b.open(url)
try:
r.read()
except IncompleteRead, e:
print "IncompleteRead using mechanize", e
# try using urllib2
r = urllib2.urlopen(url)
try:
r.read()
except IncompleteRead, e:
print "IncompleteRead using urllib2", e
# try using requests
try:
r = requests.request('GET', url)
except IncompleteRead, e:
print "IncompleteRead using requests", e
# this function is old and I categorized it as ...
# "at least it works darnnit!", but I would really like to
# learn what's happening. Please help me put this function into
# eternal rest.
def get_rss_feed(url):
response = urllib2.urlopen(url)
read_it = True
content = ''
while read_it:
try:
content += response.read(1)
except IncompleteRead:
read_it = False
return content, response.info()
content, info = get_rss_feed(url)
feed = feedparser.parse(content)
如前所述,这不是一个关键任务问题,而是一个好奇心,因为即使我可以预期 urllib2 有这个问题,我很惊讶在 mechanize 和 requests 中也会遇到这个错误。 feedparser 模块甚至不会抛出错误,因此检查错误取决于是否存在“bozo_exception”键。
编辑:我只想提一下 wget 和 curl 都完美地执行了该功能,每次都能正确检索完整的有效负载。我还没有找到一个纯 python 方法来工作,除了我丑陋的 hack,我很想知道 httplib 后端发生了什么。巧的是,前几天我决定用斜纹布也试试这个,得到了同样的 httplib 错误。
附:还有一件事也让我觉得很奇怪。 IncompleteRead 始终发生在负载中的两个断点之一。似乎 feedparser 和 requests 在读取 926 个字节后失败,但 mechanize 和 urllib2 在读取 1854 个字节后失败。这种行为是一致的,我没有解释或理解。
最佳答案
最后,所有其他模块(feedparser
、mechanize
和 urllib2
)调用 httplib
这是抛出异常的地方。
现在,首先,我还用 wget 下载了这个文件,结果文件是 1854 字节。接下来,我尝试了 urllib2
:
>>> import urllib2
>>> url = 'http://hattiesburg.legistar.com/Feed.ashx?M=Calendar&ID=543375&GUID=83d4a09c-6b40-4300-a04b-f88884048d49&Mode=2013&Title=City+of+Hattiesburg%2c+MS+-+Calendar+(2013)'
>>> f = urllib2.urlopen(url)
>>> f.headers.headers
['Cache-Control: private\r\n',
'Content-Type: text/xml; charset=utf-8\r\n',
'Server: Microsoft-IIS/7.5\r\n',
'X-AspNet-Version: 4.0.30319\r\n',
'X-Powered-By: ASP.NET\r\n',
'Date: Mon, 07 Jan 2013 23:21:51 GMT\r\n',
'Via: 1.1 BC1-ACLD\r\n',
'Transfer-Encoding: chunked\r\n',
'Connection: close\r\n']
>>> f.read()
< Full traceback cut >
IncompleteRead: IncompleteRead(1854 bytes read)
所以它正在读取所有 1854 个字节,但随后认为还有更多内容。如果我们明确告诉它只读取 1854 个字节,它就可以工作:
>>> f = urllib2.urlopen(url)
>>> f.read(1854)
'\xef\xbb\xbf<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">...snip...</rss>'
显然,这只有在我们总是提前知道确切长度时才有用。我们可以使用部分读取作为异常属性返回的事实来捕获全部内容:
>>> try:
... contents = f.read()
... except httplib.IncompleteRead as e:
... contents = e.partial
...
>>> print contents
'\xef\xbb\xbf<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">...snip...</rss>'
This blog post表明这是服务器的故障,并描述了如何使用上面的 try..except
block 对 httplib.HTTPResponse.read()
方法进行猴子修补以处理事情幕后花絮:
import httplib
def patch_http_response_read(func):
def inner(*args):
try:
return func(*args)
except httplib.IncompleteRead, e:
return e.partial
return inner
httplib.HTTPResponse.read = patch_http_response_read(httplib.HTTPResponse.read)
我应用了补丁,然后 feedparser
工作了:
>>> import feedparser
>>> url = 'http://hattiesburg.legistar.com/Feed.ashx?M=Calendar&ID=543375&GUID=83d4a09c-6b40-4300-a04b-f88884048d49&Mode=2013&Title=City+of+Hattiesburg%2c+MS+-+Calendar+(2013)'
>>> feedparser.parse(url)
{'bozo': 0,
'encoding': 'utf-8',
'entries': ...
'status': 200,
'version': 'rss20'}
这不是最好的做事方式,但它似乎有效。我在 HTTP 协议(protocol)方面不够专业,无法确定服务器是否做错了,或者 httplib
是否错误处理了边缘情况。
关于python - 使用 httplib 读取不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14149100/