簡體   English   中英

從 Reddit 獲取所有數據(帖子及其評論)的最佳方式是什么?

[英]What is the best way to fetch all data (posts and their comments) from Reddit?

我需要分析關於 subreddit 的所有評論,例如 r/dogs,比如說從 2015 年開始。 我想獲取 json 並將其存儲在 mongodb 中,以便我可以在需要時解析數據。 我檢查了 PRAW,但沒有選項可以將所有評論放在 json 中。 我只能找到獲得熱門提交的方法。

有什么辦法可以編碼嗎?

謝謝 !

您可以使用Python Pushshift.io API Wrapper (PSAW) 來獲取所有最新提交的內容,甚至可以在特定子目錄中搜索特定文本和評論,例如搜索特定子目錄中的特定文本和評論。 文檔可在此處獲得。

例如,您可以使用get_submissions() function 來獲取 2015 年以來 r/dogs 的前 1000 個提交:

import datetime as dt
import praw
from psaw import PushshiftAPI

r = praw.Reddit(...)
api = PushshiftAPI(r)

start_epoch=int(dt.datetime(2015, 1, 1).timestamp()) # Could be any date

submissions_generator = api.search_submissions(after=start_epoch, subreddit='dogs', limit=1000) # Returns a generator object
submissions = list(submissions_generator) # You can then use this, store it in mongoDB, etc.

或者,要獲得 2015 年 r/dogs 的前 1000 條評論,您可以使用search_comments() function:

start_epoch=int(dt.datetime(2015, 1, 1).timestamp()) # Could be any date
    
comments_generator = api.search_comments(after=start_epoch, subreddit='dogs', limit=1000) # Returns a generator object
comments = list(comments_generator)

如您所見,PSAW 仍然使用 PRAW,因此返回 PRAW 對象用於提交和評論,這可能很方便。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM