0

我正在尝试创建一个脚本,它会通知我 sub-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

这可以正常工作。那么为什么它在脚本中不起作用呢?

4

1 回答 1

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. 
于 2017-12-27T19:14:02.037 回答