我试图获得所有的/r/politics的帖子在过去两个月的所有评论和用户详细信息。我怎么用婴儿车做这个?
我应该浏览一下get_hot()
中的帖子吗?有什么办法吗?有什么时间戳方法可以利用吗?
最佳答案
您可以使用Cloudsearch syntax在两个时间戳之间搜索。语法为:
timestamp:START_UNIX_TIMESTAMP..END_UNIX_TIMESTAMP
如果您期望返回的条目少于1000个,那么只需设置一个搜索就可以了。不过,搜索查询被限制为1000个请求,因此如果超过预期的帖子数,则需要一些特殊的逻辑。
若要搜索过去2个月内的任何帖子,请尝试:
import time
current_timestamp = time.time()
# 60 seconds * 60 minutes * 24 hours * 60 days = 2 months
two_months_timestamp = current_timestamp - (60 * 60 * 24 * 60)
query = 'timestamp:{}..{}'.format(current_timestamp, two_months_timestamp)
results = reddit.subreddit('test').search(query, sort='new')
如果需要超过1000个,我建议您获取搜索结果中最后一项的时间戳,然后存储该项的时间戳并搜索
timestamp:<current_timestamp>..<last_item_timestamp>
,然后重复此操作,直到没有更多的结果。关于python - 从特定的subreddit(使用PRAW)获取过去两个月的所有提交?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40960449/