问题描述
我正在使用Pandas尝试在subreddit中为每个帖子打印评论。我正在使用PRAW文档以及以下代码,
submission = reddit.submission(id=tmp_id)
submission.comments.replace_more(limit=None)
for comment in submission.comments.list():
print(comment.body)
尝试执行此操作。下面是我的完整代码。该程序为每个帖子打印“评论检索错误”。有人可以帮助指导我在这里做错什么吗?谢谢!
import requests
import json
import pandas as pd
import praw
from prawcore.exceptions import ResponseException
import time
import csv
first_epoch = 1577836799
last_epoch = 1577836800
# Read my keys
with open("keys") as f:
keys = json.load(f)
# Initialize reddit API
reddit = praw.Reddit(client_id=keys['client_id'],client_secret=keys['client_secret'],user_agent=keys['user_agent'])
def getPushshiftData(after,before):
url = 'https://api.pushshift.io/reddit/submission/search/?sort_type=created_utc&sort=asc&subreddit=SpotifyPlaylists&after='+ str(after) +"&before"+str(before)+"&size=1000"
print(url)
r = requests.get(url)
data = json.loads(r.text)
return data['data']
timestamps = list()
post_ids = list()
title = list()
post_title = list()
full_link = list()
description = list()
after = first_epoch
while int(after) < last_epoch:
data = getPushshiftData(after,last_epoch)
for post in data:
try:
tmp_time = post['created_utc']
timestamps.append(tmp_time)
except (KeyError,IndexError):
timestamps.append("error")
try:
tmp_id = post['id']
post_ids.append(tmp_id)
print(tmp_id)
except (KeyError,IndexError):
post_ids.append("error")
try:
submission = reddit.submission(id=tmp_id)
submission.comments.replace_more(limit=None)
for comment in submission.comments.list():
print(comment.body)
except:
print("error in comment retrieval")
after = timestamps[-1]
print([str(len(post_ids)) + " posts collected so far."])
time.sleep(0.1)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)