使用feedparser按日期限制RSS元素.[蟒蛇]

Rec*_*ion 3 python feedparser

我迭代一个RSS提要,因为_file是feed

d = feedparser.parse(_file)
for element in d.entries: 
    print repr(element.date)
Run Code Online (Sandbox Code Playgroud)

日期输出就像这样

u'Thu, 16 Jul 2009 15:18:22 EDT'
Run Code Online (Sandbox Code Playgroud)

我似乎无法理解如何实际量化上述日期输出,所以我可以用它来限制饲料元素.我所以我要问的是如何才能获得实际的时间,所以我可以说如果超过7天,请跳过这个元素.

Fog*_*ird 5

feedparser应该从Python的时间模块中为您提供struct_time对象.我猜它不能识别日期格式,所以给你原始字符串.

请参阅此处,了解如何添加对解析格式错误的时间戳的支持:

http://pythonhosted.org/feedparser/date-parsing.html

如果你设法让它给你struct_time,你可以在这里阅读更多相关内容:

http://docs.python.org/library/time.html#time.struct_time

struct_time对象拥有您需要的一切.他们有这些成员:

time.struct_time(tm_year=2010, tm_mon=2, tm_mday=4, tm_hour=23, tm_min=44, tm_sec=19, tm_wday=3, tm_yday=35, tm_isdst=0)

我通常将结构转换为秒,如下所示:

import time
import calendar

struct = time.localtime()
seconds = calendar.timegm(struct)
Run Code Online (Sandbox Code Playgroud)

然后你可以做常规数学来看看已经过了多少秒,或者使用datetime模块来做timedeltas.