【问题标题】:feedparser attribute errorfeedparser 属性错误
【发布时间】: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


【解决方案1】:

我打印了story.keys(),但我只得到了。

['summary_detail', 'published_parsed', 'links', 'title', 'media_thumbnail',
 'summary', 'guidislink', 'title_detail', 'href', 'link', 'published', 'id']

也许story.published 是你所需要的。

【讨论】:

  • 正是我需要的。谢谢你。出于某种原因,我认为提要中的子元素名称与我需要检索的名称相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2021-03-10
相关资源
最近更新 更多