如何使用 Python feedparser 访问 RSS 项目的 pubDate?

Mar*_*rch 0 python rss feedparser rss2 python-2.7

此示例 RSS 提要中可选项目元素pubDate包含在所有条目中。但它不能作为 Python 模块feedparser 中的 item 元素使用。这段代码:

import feedparser
rss_object = feedparser.parse("http://cyber.law.harvard.edu/rss/examples/rss2sample.xml")
for entry in rss_object.entries:
    print entry.pubDate
Run Code Online (Sandbox Code Playgroud)

导致错误,AttributeError: object has no attribute 'pubDate'但我可以成功执行print entry.description并查看所有描述标签的内容。

Mar*_*rch 6

feedparser是一个自以为是的解析器,而不是简单地在字典中返回 XML。的文本pubDate可用作entries[i].published

此条目首次发布的日期,作为与原始提要中发布的格式相同的字符串。

工作代码:

for entry in rss_object.entries:
    print entry.published
Run Code Online (Sandbox Code Playgroud)

注意:published根据提要的格式从几个可能的 XML 标记之一中提取。有关列表,请参阅参考手册

本手册还声称 pubDate 元素在entries[i].published_parsed. 里面published_parsed是一个time.struct_time对象;如果原始提要包含时区,您可能需要自己重新解析日期以维护时区信息