You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python PRAW报'ListingGenerator' has no attribute 'top'错解决

错误原因

触发'ListingGenerator' object has no attribute 'top'是因为重复调用了.top()方法:

  • 执行subreddit = reddit.subreddit("unexpected").top(time_filter="week")时,.top()返回的是可迭代的帖子生成器(ListingGenerator实例),不是子版块(Subreddit)对象
  • 后续在这个生成器对象上再次调用.top(limit = 25),但ListingGenerator类本身没有定义top方法,直接抛出属性错误。
    另外原代码里for循环下的print语句没有缩进,运行时也会触发缩进语法错误。
修复代码

Subreddit.top()方法同时支持time_filter和limit参数,把两个参数合并到同一次调用即可:

import praw

reddit = praw.Reddit(
    client_id = "",
    client_secret = "",
    user_agent = "",
    username = "",
    password = ""
)

# 先获取子版块实例,再单次调用top传入所有筛选参数
subreddit = reddit.subreddit("unexpected")
top_weekly_posts = subreddit.top(time_filter="week", limit=25)

for submission in top_weekly_posts:
    print(submission.url)
参数说明

time_filter支持的可选值对应不同时间范围的热门排序:

  • hour:1小时内
  • day:24小时内
  • week:一周内
  • month:一个月内
  • year:一年内
  • all:全站历史

内容的提问来源于stack exchange,提问作者tomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 23:33:21