【问题标题】:What are the different sorting types allowed in Reddit's JSON API?Reddit 的 JSON API 允许哪些不同的排序类型?
【发布时间】:2013-02-21 18:15:21
【问题描述】:

我遇到了一个函数,它使用它的 API 从 reddit 中获取热门图片。

sr='pics'
sorting=''
url = r'http://www.reddit.com/r/{sr}/{top}.json'.format(sr=sr, top=sorting)

但是我发现最初的编码员很早就发布了这样的内容:

#'sorting' is whether or not the sorting of the reddit should be customized or not,
# if it is: Allowed passing params/queries such as t=hour, week, month, year or all"""

我不确定如何使用这些其他“排序”命令,或者在哪里可以找到更多关于如何使用它们的信息。我什至不确定我是否应该更多地了解“排序”作为 json/reddit/python 表示法的一部分。

我想做的不仅是通过另一个查询获取由sorting=''定义的顶部图像,而且还获取底部图像(如果可能,以其他方式排序)。

【问题讨论】:

标签: python json api reddit


【解决方案1】:

如果您正常访问 subreddit 并单击顶部,然后从“本周”链接,则 URL 是

http://www.reddit.com/r/pics/top/?sort=top&t=week

如果我将 top/ 更改为 top.json

http://www.reddit.com/r/pics/top.json?sort=top&t=week

我得到了 JSON 等价物。 但是,来自周 URL 的有争议的链接是

http://www.reddit.com/r/pics/controversial/?sort=controversial&t=week

变成了

http://www.reddit.com/r/pics/controversial.json?sort=controversial&t=week

因此,这次测试似乎不可能触底。

就像@Martijn 评论的那样。这是一个 reddit API 问题,而不是 Python 或 JSON 问题。

【讨论】:

  • 好的,谢谢,是的,我刚刚解决了这个问题,也发现了这个问题:reddit.com/dev/api#GET_{sort} 我认为你是对的:虽然可能无法达到最低点。
【解决方案2】:

要坚持使用您的 url 变量:url = 'http://www.reddit.com/r/{sr}/{top}.json' 您的方法需要分配上述数组中的参数之一,例如“上升”

你应该设置一个默认值,这样方法调用就不会在没有传递参数的情况下失败,例如:

def get_data_from_reddit_api(sr="default subreddit value", top="hot")
  #url = ...
  #rest of reddit api request & subsequent parsing of the json feed
end

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 2022-12-19
    • 2016-09-17
    • 1970-01-01
    • 2015-10-08
    • 2015-04-28
    • 2020-10-13
    • 2019-09-14
    • 2023-03-19
    相关资源
    最近更新 更多