打印 reddit 抓取到文件

print reddit scraping to file

我希望将来自 Reddit 的查询结果重定向到一个文件。 谢谢

for comments in subreddit.stream.comments(skip_existing = True):
    if (time.time() - start_time) < 10:
        print(comments.body, comments.author)

如果这是您文件的唯一输出,python myPythonFile.py > fileToWrite.txt 是 shell 环境中最简单的解决方案。

否则您可以从 Python:

写入文件
# open the file
with open('fileToWrite', 'w', encoding='utf-8') as f: 
    for comments in subreddit.stream.comments(skip_existing = True): 
        if (time.time() - start_time) < 10:
            f.write("{}, {}\n".format(comments.body, comments.author))