已正确创建Reddit PRAW实例仍无法连接Pushshift.io求助
解决Pushshift连接失败问题及可用示例
问题根源
- Pushshift官方的
pushshift.io服务已停止维护,原端点不稳定甚至无法访问,这是你遇到连接错误的核心原因 - PSAW库默认调用旧的官方端点,自然无法正常连接
修复方案与可用代码示例
方案1:修改PSAW的端点配置(继续用PSAW)
手动指定第三方维护的Pushshift镜像端点,示例代码:
from psaw import PushshiftAPI import praw # 初始化PRAW实例(确保你的认证参数正确) reddit_ = praw.Reddit( client_id='你的client_id', client_secret='你的client_secret', user_agent='你的user_agent' ) # 指定可用的Pushshift镜像端点 api = PushshiftAPI(reddit_, requestor_kwargs={'base_url': 'https://api.pushshift.io'}) # 测试获取subreddit帖子 try: # 获取r/python板块最近10条帖子 submissions = api.search_submissions(subreddit='python', limit=10) for sub in submissions: print(sub.title) except Exception as e: print(f"请求出错:{str(e)}")
方案2:改用pmaw库(推荐)
PSAW已停止维护,pmaw是适配新端点的替代库,使用更稳定:
- 先安装pmaw:
pip install pmaw
- 使用示例:
from pmaw import PushshiftAPI import praw # 初始化PRAW(可选,pmaw可单独使用) reddit_ = praw.Reddit( client_id='你的client_id', client_secret='你的client_secret', user_agent='你的user_agent' ) # 初始化pmaw的PushshiftAPI,默认使用可用端点 api = PushshiftAPI(praw=reddit_) # 测试获取评论 comments = api.search_comments(subreddit='python', limit=5) for comment in comments: print(comment['body'])
注意事项
- Pushshift镜像端点也有请求频率限制,避免短时间内发起大量请求
- 如果某个镜像端点无法访问,可以搜索当前可用的Pushshift公共镜像替换
内容的提问来源于stack exchange,提问作者big_haas
相关产品推荐
相关产品推荐

