我正在尝试创建一个脚本,通知我子reddit中的新帖子。代码的核心是:
sub = r.get_subreddit('indianwriters',fetch=True)
time.sleep(5)
posts = sub.get_new()
while True:
try:
time.sleep(2.5)
post = next(posts)
print post.id,post.title
然而,缺少第一篇文章。但是当我逐行输出时,如下所示:
sub = r.get_subreddit('indianwriters',fetch=True)
time.sleep(5)
posts = sub.get_new()
post = next(posts)
print post.id,post.title
这可以正常工作。那么为什么它不能在脚本中运行呢?
答案 0 :(得分:0)
你在这里重新发明轮子。最简单的方法是使用流。您的代码如下所示:
subreddit = reddit.subreddit('indianwriters')
for post in subreddit.stream.submissions():
print("Found post! Title: " + post.title + " Link: " + post.permalink)
#Do whatever you want to notify you here.