我一直在从特定网站获取RSS订阅源时遇到问题.我最后编写了一个相当丑陋的程序来执行此功能,但我很好奇为什么会发生这种情况以及是否有任何更高级别的接口正确处理此问题.这个问题实际上不是一个显示限制因素,因为我不需要经常检索提要.
我已经阅读了一个捕获异常并返回部分内容的解决方案,但由于不完整的读取实际检索的字节数不同,我不确定这样的解决方案是否真的有效.
#!/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: …Run Code Online (Sandbox Code Playgroud)