我正在为RSS提要编写Python解析器脚本。我正在使用feedparser,然而,我被困在解析来自FeedBurner的提要上。现在谁还需要FeedBurner呢?不管怎样..。
例如,我找不到解析的方法
http://feeds.wired.com/wired/index
http://feeds2.feedburner.com/ziffdavis/pcmag
当我把它们放到feedparser库中时,似乎不起作用。尝试将?fmt=xml或?format=xml放在urls的末尾,但仍然没有得到xml格式。
我是否需要使用BeautifulSoup等html解析器来解析FeedBurner提要?最好是,有没有python公共解析器或聚合器脚本来处理这个问题?
任何提示或帮助都将非常感谢。
发布于 2011-04-19 21:48:17
您可能有版本问题,或者您没有正确使用API --查看您的错误消息会有所帮助。例如,以下代码适用于Python 2.7和feedparser 5.0.1:
>>> import feedparser
>>> url = 'http://feeds2.feedburner.com/ziffdavis/pcmag'
>>> d = feedparser.parse(url)
>>> d.feed.title
u'PCMag.com: New Product Reviews'
>>> d.feed.link
u'http://www.pcmag.com'
>>> d.feed.subtitle
u"First Look At New Products From PCMag.com including Lab Tests, Ratings, Editor's and User's Reviews."
>>> len(d['entries'])
30
>>> d['entries'][0]['title']
u'Canon Color imageClass MF9280cdn'
并使用另一个URL:
>>> url = 'http://feeds.wired.com/wired/index'
>>> d = feedparser.parse(url)
>>> d.feed.title
u'Wired Top Stories'
>>> d.feed.link
u'http://www.wired.com/rss/index.xml'
>>> d.feed.subtitle
u'Top Stories<img src="http://www.wired.com/rss_views/index.gif" />'
>>> len(d['entries'])
30
>>> d['entries'][0]['title']
u'Heart of Dorkness: LARPing Goes Haywire in <em>Wild Hunt</em>'
发布于 2012-02-13 09:45:08
我知道这个问题很老了,但是我想它会对任何碰巧遇到这个问题的人有帮助,通过搜索一个解析feedburner RSS提要的解决方案来粘贴我从Cracked.com feedburner获得最新条目的简单代码。我已经在其他一些网站上测试过了,它工作得很好。
def GetRSS('RSSurl'):
url_info = urllib.urlopen(RSSurl)
if (url_info):
xmldoc = minidom.parse(url_info)
if (xmldoc):
url = xmldoc.getElementsByTagName('link').firstChild.data
title = xmldoc.getElementsByTagName('title').firstChild.data
print url, print title
只需用feedburner页面的地址替换RSSurl即可。此外,正如您可能看到的,如果有任何其他您想要的元素,您只需在那里添加额外的getElementsByTagName行,即可获得您想要的任何内容。
编辑:另外,据我所知,它可以处理几乎所有的RSS提要。
https://stackoverflow.com/questions/5722963
复制相似问题