【发布时间】:2020-08-16 01:25:54
【问题描述】:
所以我正在尝试使用 praw 获取 reddit 帖子数据并将其转换为 JSON Lines 文件。
我需要的是这样的:
{"context": ["Cross your redstone wires - Snapshot 20w18a is out", "But how will people get a blood spot effect now if the redstone default is a cross again?"], "response": ["Debug Stick?"], "id": "gabsj3"}
{"context": ["Cross your redstone wires - Snapshot 20w18a is out", "But how will people get a blood spot effect now if the redstone default is a cross again?", "Debug Stick?"], "response": ["My guess is the dot is flat out gone\n\nThere's no way for it to exist so why would they leave it in"], "id": "gabsj3"}
{"context": ["Cross your redstone wires - Snapshot 20w18a is out", "But how will people get a blood spot effect now if the redstone default is a cross again?", "Debug Stick?", "My guess is the dot is flat out gone\n\nThere's no way for it to exist so why would they leave it in"], "response": ["No, it's still in the game. Use the debug stick to set all sides to `none`"], "id": "gabsj3"}
所以上下文包含 ["POST TITLE", "FIRST LEVEL COMMENT", "SECOND LEVEL COMMENT", "ETC..."] 并且响应包含最后级别的评论。在这个post on reddit,应该是:
{"context": ["Cross your redstone wires - Snapshot 20w18a is out", "But how will people get a blood spot effect now if the redstone default is a cross again?", "Debug Stick?", "My guess is the dot is flat out gone\n\nThere's no way for it to exist so why would they leave it in", "No, it's still in the game. Use the debug stick to set all sides to `none`"], "response": ["Huh, alright"], "id": "gabsj3"}
但是我的代码的输出是这样的:
{"context": ["Cross your redstone wires - Snapshot 20w18a is out", "But how will people get a blood spot effect now if the redstone default is a cross again?"], "response": ["Debug Stick?", "I think we can still use resource packs to change it back into a dot, I don't know so don't quote me on that", "I honestly think the cross redstone looks a bit more like a splatter."], "id": "gabsj3"}
这是我的代码:
import praw
import jsonlines
reddit = praw.Reddit(client_id='-', client_secret='-', user_agent='user_agent')
max = 1000
sequence =1
for post in reddit.subreddit('minecraft').new(limit=max):
data = []
title = []
comment = []
response = []
post_id = post.id
titl = post.title
# print("https://www.reddit.com/"+post.permalink)
print("Fetched "+str(sequence) + " posts .. ")
title.append(titl)
try:
submission = reddit.submission(id=post_id)
submission.comments.replace_more(limit=None)
sequence = sequence + 1
for top_level_comment in submission.comments:
cmnt_body = top_level_comment.body
comment.append(cmnt_body)
for second_level_comment in top_level_comment.replies:
response.append(second_level_comment.body)
context = [title[0],comment[0]]
data.append({"context":context,"response":response,"id":post_id})
response = []
# print(data[0])
with jsonlines.open('2020-04-30_12.jsonl', mode='a') as writer:
writer.write(data.pop())
comment.pop()
title.pop()
except Exception :
pass
【问题讨论】:
-
到底是什么问题?你做过调试吗?请参阅How to Ask、help center。
-
您的脚本似乎缺少
import jsonlines、import praw和reddit的定义。它也有一些语法错误,这使得运行和调试很麻烦。