【发布时间】:2014-07-06 22:34:36
【问题描述】:
我正在尝试从 BBC rss 提要中检索一些新闻并将某些部分保存在本地 xml 中(尽管此代码仅打印它)。我似乎能够检索除 pubDate 之外的所有内容。我得到错误
"File "/Library/Python/2.7/site-packages/feedparser.py", line 416, in __getattr__
raise AttributeError, "object has no attribute '%s'" % key
AttributeError: object has no attribute 'pubDate'"
我不确定为什么我想要检索的其他所有内容都没有造成任何问题。代码如下:
import feedparser
import xml.etree.cElementTree as ET
from xml.dom import minidom
BBCHome = feedparser.parse ('http://feeds.bbci.co.uk/news/rss.xml')
def prettify(elem):
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
root = ET.Element('root')
for story in BBCHome.entries:
item = ET.SubElement(root,'item')
title = ET.SubElement(item,'title')
title.text = story.title
# why doesn't pubDate work?
pubDate = ET.SubElement (item,'pubDate')
pubDate.text = story.pubDate
description = ET.SubElement(item,'description')
description.text = story.description
link = ET.SubElement(item,'link')
link.text = story.link
print prettify(root)
阅读本页:https://pythonhosted.org/feedparser/namespace-handling.html 我认为这可能与名称空间有关,但我不太明白。 我查看了原始提要,它似乎只是项目的另一个子元素,类似于描述或标题。
如果我能找到解决此问题的方法以及为什么它不起作用,我将不胜感激。 谢谢。
【问题讨论】:
-
也许如果你
print story那么你会得到里面的东西 - 也许它有不同的名字。 -
谢谢,马上试试
标签: python xml rss feedparser