4

I need to display RSS-feeds with Python, Atom for the most part. Coming from PHP, where I could get values pretty fast with $entry->link i find lxml to be much more precise, faster, albeit complicated. After hours of probing I got this working with the arstechnica-feed:

def GetRSSFeed(url):
    out = []
    feed = urllib.urlopen(url)
    feed = etree.parse(feed)
    feed = feed.getroot()
    for element in feed.iterfind(".//item"):
        meta = element.getchildren()
        title = meta[0].text
        link = meta[1].text
        for subel in element.iterfind(".//description"):
            desc = subel.text
            entry = [title,link,desc]
            out.append(entry)
    return out

Could this be done any easier? How can I access tags directly? Feedparser gets the job done with one line of code! Why?

4

2 回答 2

9

查看 feedparser 库。它为您提供了一个格式良好的 RSS 对象。

> import feedparser
> feed = feedparser.parse('http://feeds.marketwatch.com/marketwatch/marketpulse/')
> print feed.keys()
['feed',
 'status',
 'updated',
 'updated_parsed',
 'encoding',
 'bozo',
 'headers',
 'etag',
 'href',
 'version',
 'entries',
 'namespaces']

>  len(feed.entries)
    30
于 2012-06-22T14:37:30.733 回答
3

您可以尝试speedparser,这是Universal Feed Parser的实现lxml。虽然仍处于测试阶段。

于 2013-06-19T06:32:01.597 回答