Beautifulsoup/Python解析RSS的奇怪现象

3 投票
1 回答
1469 浏览
提问于 2025-04-15 18:40

我正在用BeautifulSoup解析一个RSS/播客的源,其他部分都运行得很好,但我就是解析不了'pubDate'这个字段。

data = urllib2.urlopen("http://www.democracynow.org/podcast.xml")
dom = BeautifulStoneSoup(data, fromEncoding='utf-8')
items = dom.findAll('item');

for item in items:
    title = item.find('title').string.strip()
    pubDate = item.find('pubDate').string.strip()

标题部分解析得很好,但到了pubDate那里,就出现了:

追踪信息(最近的调用在最后): 文件 "", 第2行, 在 AttributeError: 'NoneType'对象没有'string'这个属性

不过,当我下载一个XML文件的副本并把'pubDate'改成其他名字,再解析一次,就能正常工作了。难道pubDate在Python里是个保留变量之类的吗?

谢谢,

g

1 个回答

3

这个方法可以用 item.find('pubdate').string.strip() 来实现。你为什么不试试 feedparser 呢?

撰写回答