【问题标题】:Is there any way to parse through reddit comments and replies through JSON and not through PRAW?有没有办法通过 JSON 而不是 PRAW 来解析 reddit 评论和回复?
【发布时间】: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,在您的情况下)有些可能存在,也可能不存在。

标签: python json reddit


【解决方案1】:

如果您正在使用 praw 并且想要获取所有 cmets,您可以像这样获取所有 cmets:

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 是你最好的选择。

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2021-08-26
    相关资源
    最近更新 更多