2017-08-22 28 views
0

我想通过分析评论和对每个评论的回复进行解析。但是,我试图避免使用PRAW。这是我现在可以在subreddit中显示每篇文章的标题的代码。但是,如何访问评论栏和答复?有没有什么办法通过JSON而不是通过PRAW解析reddit的评论和回复?

import requests 
import json 

r = requests.get('http://www.reddit.com/r/wallstreetbets/new.json?count=500', headers = {'User-agent': 'Chrome'}) 
r_comments = requests.get('https://www.reddit.com/r/wallstreetbets/comments.json') 
theJSON = json.loads(r.text) 
theJSON_comments = json.loads(r_comments.text) 
titles = [] 
#print(theJSON) 
#prints the titles 
for child in theJSON['data']['children']: 
    titles.append(child['data']['title']) 
    #print(child['data']['title']) 

for child2 in theJSON_comments['data']['children']: 
    print(child2['data'][0]) 
+0

new.json不会给你一个可预测的评论结构 - 通常回复嵌套在评论的[data] [children]部分,但取决于你的参数(count = 500,在你的情况下)可能会或可能不会。 –

回答

0

如果您使用praw,并希望得到所有你可以得到所有像这样的评论的评论:

submission = reddit.submission(id=<submission_id>) 
submission.comments.replace_more(limit=None) 
all_comments = submission.comments.list() 

然后all_comments是你可以使用的字典。不是json,但可以保存为json文件。

如果你不想使用praw,你可以用你想要的任何语言手动使用reddit api。不过,我有一个blog post,它说如何在Javascript中设置它。

我认为PRAW会真的帮助你在这里,否则的话reddit API是你最好的选择。

相关问题