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])
new.json不會給你一個可預測的評論結構 - 通常回復嵌套在評論的[data] [children]部分,但取決於你的參數(count = 500,在你的情況下)可能會或可能不會。 –