【问题标题】:Parsing out all timestamps in RSS feed using feedparser使用 feedparser 解析 RSS 提要中的所有时间戳
【发布时间】:2013-05-07 07:50:39
【问题描述】:

我对 Python 中的 feedparser 库相当陌生。

尝试从 RSS 提要中解析出完整的时间戳列表,我目前有:

import feedparser
from time import gmtime, strftime

d = feedparser.parse('http://www.bloomberg.com/feed/podcast/taking-stock.xml')
dPub = d.entries[0].published   # out: u'Mon, 06 May 2013 08:19:36 -0400'
dPubPretty = strftime(dPub, gmtime())
print dPubPretty    # out: Mon, 06 May 2013 08:19:36 -0400

# loop over d.entries[0:] - ???
# for all d.entries...

d.entries[1].published  # out: u'Mon, 06 May 2013 08:16:15 -0400'
d.entries[2].published  # out: u'Fri, 03 May 2013 09:01:50 -0400'

我想遍历所有 d.entries 并输出时间戳列表,因此应用 strftime() 后,输出将类似于:

# output goal:
Mon, 06 May 2013 08:19:36 -0400
Mon, 06 May 2013 08:16:15 -0400
Fri, 03 May 2013 09:01:50 -0400
...

参考这些文档

feedparser - 内容规范化:http://pythonhosted.org/feedparser/content-normalization.html#advanced-normalization

时间 - 时间访问和转换:http://docs.python.org/2/library/time.html#time.strftime

【问题讨论】:

    标签: python date time feedparser


    【解决方案1】:

    尝试遍历每个条目

    import feedparser
    from time import gmtime, strftime
    
    d = feedparser.parse('http://www.bloomberg.com/feed/podcast/taking-stock.xml')
    
    for entry in d.entries:
        dPub = entry.published  
        dPubPretty = strftime(dPub, gmtime())
        print dPubPretty    
    

    你会得到以下输出:

    Mon, 06 May 2013 08:19:36 -0400
    Mon, 06 May 2013 08:16:15 -0400
    Fri, 03 May 2013 09:01:50 -0400
    Fri, 03 May 2013 08:57:55 -0400
    Fri, 03 May 2013 08:54:21 -0400
    Thu, 02 May 2013 10:04:42 -0400
    Thu, 02 May 2013 09:38:42 -0400
    ...
    Mon, 18 Mar 2013 08:03:27 -0400
    Mon, 18 Mar 2013 08:01:21 -0400
    

    【讨论】:

    • 你不需要range(len(d.entries));你可以简单地做for i in d.entries: 然后dPub = i.published
    • 以上两个,jabaldonedo 的完整脚本和 B. Khalid 的评论,正是我所需要的,它们都工作(测试)! Khalid 的更新使脚本更短了...非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2015-07-29
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多