2011-04-19 70 views
4

我正在爲RSS提要編寫一個Python解析器腳本。我正在使用feedparser,但是,我堅持解析來自FeedBurner的提要。現在誰需要FeedBurner?不管怎麼說..Python RSS解析器,也處理FeedBurner

例如,我找不到方法來解析

http://feeds.wired.com/wired/index

http://feeds2.feedburner.com/ziffdavis/pcmag

當我把那些進入feedparser庫,似乎並沒有工作。 嘗試在URL末尾添加?fmt = xml或?format = xml,但仍未獲取xml格式。

我是否需要使用諸如BeautifulSoup這樣的html解析器來解析FeedBurner提要?最好,是否有一個python公共解析器或聚合器腳本處理這已經?

任何提示或幫助將不勝感激。

回答

4

這可能是您的版本問題,或者您使用的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 = '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>' 
+0

遵循您的指導作品。謝謝你。我一定是錯誤的輸入了一些東西,因爲我只是得到了d ['entries']的空結果 – DavidL 2011-04-19 22:12:31

+0

@DavidL:現在很高興它現在可以工作。我對「錯誤的錯誤」非常熟悉。 :-) – ars 2011-04-24 04:48:53

2

我知道這個問題已經很老了,但我想這將是通過搜索解析feedburner RSS feed來粘貼一個簡單的代碼的解決方案,幫助任何遇到它的人都能獲得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 

只需將RSSurl替換爲feedburner頁面的任何地址即可。另外,正如你可能看到的那樣,如果還有其他任何你想要的元素,你可以在其中添加一個額外的getElementsByTagName行,以及任何你想得到的。

編輯:據我所知,幾乎可以與任何RSS源一起使用。