使用Universal Feed Parser获取日期时遇到问题

1 投票
2 回答
4043 浏览
提问于 2025-04-15 12:23

看起来 http://portland.beerandblog.com/feed/atom/ 这个链接有问题(0.92和2.0版本的RSS源也是如此)。

最新版本的Universal Feed Parser(可以从这里找到)没有找到任何日期。

    <title>Beer and Blog Portland</title>
    <atom:link href="http://portland.beerandblog.com/feed/" rel="self" type="application/rss+xml" />
    <link>http://portland.beerandblog.com</link>
    <description>Bloggers helping bloggers over beers in Portland, Oregon</description>
    <pubDate>Fri, 19 Jun 2009 22:54:57 +0000</pubDate>
    <generator>http://wordpress.org/?v=2.7.1</generator>
    <language>en</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
                    <item>
            <title>Widmer is sponsoring our beer for the After Party!!</title>
            <link>http://portland.beerandblog.com/2009/06/19/widmer-is-sponsoring-our-beer-for-the-after-party/</link>
            <comments>http://portland.beerandblog.com/2009/06/19/widmer-is-sponsoring-our-beer-for-the-after-party/#comments</comments>
            <pubDate>Fri, 19 Jun 2009 22:30:35 +0000</pubDate>
            <dc:creator>Justin Kistner</dc:creator>

            <category><![CDATA[beer]]></category>

我正在尝试

        try:
            published = e.published_parsed
        except:
            try:
                published = e.updated_parsed
            except:
                published = e.created_parsed

但是失败了,因为我无法获取到日期。

有没有什么好的方法可以合理地提取日期呢?

谢谢!

2 个回答

1

使用裸露的 except 可能会掩盖你代码中的问题。假设(我不使用提要解析器)AttributeError 是你应该检查的特定异常,试试这个:

try:
    published = e.published_parsed
except AttributeError:
    try:
        published = e.updated_parsed
    except AttributeError:
        published = e.created_parsed

无论如何,别说“它失败了”,请展示错误信息和追踪记录。

编辑 我下载了最新的版本(也就是不是从svn下载的),并按照文档中的示例操作,结果是这样的:

C:\feedparser>\python26\python
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import feedparser
>>> d = feedparser.parse('http://portland.beerandblog.com/feed/atom/')
>>> d.entries[0].updated
u'2009-06-19T22:54:57Z'
>>> d.entries[0].updated_parsed
time.struct_time(tm_year=2009, tm_mon=6, tm_mday=19, tm_hour=22, tm_min=54, tm_sec=57, tm_wday=4, tm_yday=170, tm_isdst=0)
>>> d.entries[0].title
u'Widmer is sponsoring our beer for the After Party!!'
>>> d.entries[0].published
u'2009-06-19T22:30:35Z'
>>> d.entries[0].published_parsed
time.struct_time(tm_year=2009, tm_mon=6, tm_mday=19, tm_hour=22, tm_min=30, tm_sec=35, tm_wday=4, tm_yday=170, tm_isdst=0)
>>>

就像我说的,我对RSS和Atom之类的东西不太了解,但对我来说似乎很简单。只是我不明白你是从哪里得到 <pubDate> 标签和arpanet风格的时间戳的;据我所知,原始源代码中并没有这个——它有 <published> 和ISO时间戳:

>>> import urllib
>>> guff = urllib.urlopen('http://portland.beerandblog.com/feed/atom/').read()
>>> guff.find('pubDate')
-1
>>> guff.find('published')
1171
>>> guff[1160:1200]
'pdated>\n\t\t<published>2009-06-19T22:30:35'
>>>

你在“e.published_parsed”中的“e”是什么?考虑展示一下如何访问feedparser的完整过程,就像我上面做的那样。

3

对我来说是有效的:

>>> e = feedparser.parse('http://portland.beerandblog.com/feed/atom/')
>>> e.feed.date
u'2009-06-19T22:54:57Z'
>>> e.feed.date_parsed
(2009, 6, 19, 22, 54, 57, 4, 170, 0)
>>> e.feed.updated_parsed
(2009, 6, 19, 22, 54, 57, 4, 170, 0)

也许你应该查看的是 e.feed.updated_parsed,而不是 e.updated_parsed

撰写回答