PRAW:与熊猫或其他一起导出到数据表;乐于接受建议



我有以下代码,它使用python脚本,调用praw并从reddit页面返回文本。我现在想:

能够获得更多评论,因为它只输出少量。我想查看评论的响应树。我想将数据的内容导出到表中我想将其导出到.csv

我是新手,所以任何建议都是有帮助的。谢谢!

import praw
import datetime as dt
reddit = praw.Reddit(client_id=,agent_id=,etc. )
submission = reddit.submission(id='7v8ob2')
comments = submission.comments
for comment in comments:
    print(20*'-')
    print('Parent ID:', comment.parent)
    print('Comment ID:', comment.id)
    print('Score:', comment.score)
    print('Created:', comment.created)
    print('Body:', comment.body)
import pandas as pd
df_rows = [[comment.parent, comment.id, comment.score, comment.created, comment.body] for comment in comments]
df = pd.DataFrame(df_rows, columns=['Parent ID', 'Comment ID', 'Score', 'Created', 'Body'])
df.to_csv('path_to_save_to.csv')

编辑:假设您无法一次展开注释生成器中所有对象的属性,则必须遍历所有注释并展开每个注释以检索其属性。

我将所有数据放在一个列表中,因为引用文档:

concat(因此追加(会创建数据的完整副本,并且不断重用此函数可能会对性能造成重大影响。如果需要对多个数据集使用该操作,请使用列表推导式。

事先将所有数据放在列表中(使用列表推导(可以避免这种性能下降。

import praw
import datetime as dt
import pandas as pd
from pandas import DataFrame
reddit = praw.Reddit(client_id=,agent_id=,etc.)
submission = reddit.submission(id='7v8ob2')
comments = submission.comments
df_rows = [[comment.parent, comment.id, comment.score, comment.created, comment.body] for comment in comments]
df = pd.DataFrame(df_rows, columns=['Parent ID', 'Comment ID', 'Score', 'Created', 'Body'])
df.to_csv('path_to_save_to.csv')

最新更新