如何在Python中获取最近2天的文章?

时间:2016-07-09 17:37:24

标签: python date python-3.x datetime

我在Python3中编写一个程序来解析新闻。解析后,每篇文章都有一个日期对象(例如(2016,7,9))。

仅保存过去2天内发布的文章的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

正如你所说,这是你的回答:

import datetime
datetime.datetime.utcnow().date() - datetime.timedelta(days=2)

答案 1 :(得分:0)

如果是文章列表:

import arrow

threshold = arrow.now().replace(days=-2)
filtered_articles = [a for a in articles if arrow.get(a['article_date')) > threshold]