【发布时间】:2017-08-23 08:32:58
【问题描述】:
我正在尝试解析 reddit cmets 以及对每个评论的回复。但是,我试图避免使用 PRAW。这是我现在拥有的代码,用于在 subreddit 中显示每个帖子的标题。但是,我如何访问评论字段及其回复?
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])
【问题讨论】:
-
new.json 不会给你一个可预测的评论结构——通常回复嵌套在评论的 [data][children] 部分,但取决于你的参数(count=500,在您的情况下)有些可能存在,也可能不存在。