PRAW:get_submission工作不一致

时间:2014-01-02 19:39:12

标签: python python-3.x reddit praw

该脚本从Reddit的特定注释中获取信息。 它取决于给定的固定链接是否有效。

import praw
from pprint import pprint
clipboard = ['permalink']
com = r.get_submission(clipboard).comments[0]
pprint(vars(com))

此永久链接有效:http://www.reddit.com/r/redditgetsdrawn/comments/1tvjmr/i_got_a_new_job_recently_and_would_love_to_have/cec9wh4

这个不是:http://www.reddit.com/r/redditgetsdrawn/comments/1ts9hi/surprise_me_thanks_in_advance/cec0897?context=3

我觉得以?context = 3结尾的永久链接或其他不寻常的结尾都有特殊的特征会搞砸.get_submission。

这是错误报告:

Traceback (most recent call last):
  File "C:\Users\Aymeric\Python33\Scripts\brdg\brdg.py", line 14, in <module>
    com = r.get_submission(clipboard).comments[0]
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\__init__.py", line 875, in get_submission
    comment_sort=comment_sort)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\decorators.py", line 320, in wrapped
    return function(cls, *args, **kwargs)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\objects.py", line 837, in from_url
    s_info, c_info = reddit_session.request_json(url, params=params)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\decorators.py", line 158, in wrapped
    return_value = function(reddit_session, *args, **kwargs)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\__init__.py", line 476, in request_json
    response = self._request(url, params, data)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\__init__.py", line 338, in _request
    cache_key = (normalize_url(request.url), tuple(key_items))
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\helpers.py", line 140, in normalize_url
    if url.endswith('.json'):
AttributeError: 'list' object has no attribute 'endswith'

编辑:以下是我将列表更改为字符串时的结果:

Traceback (most recent call last):
  File "C:\Users\Aymeric\Python33\lib\json\decoder.py", line 368, in raw_decode
    obj, end = self.scan_once(s, idx)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Aymeric\Python33\Scripts\brdg\brdg.py", line 14, in <module>
    com = r.get_submission(clipboard).comments[0]
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\__init__.py", line 875, in get_submission
    comment_sort=comment_sort)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\decorators.py", line 320, in wrapped
    return function(cls, *args, **kwargs)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\objects.py", line 837, in from_url
    s_info, c_info = reddit_session.request_json(url, params=params)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\decorators.py", line 158, in wrapped
    return_value = function(reddit_session, *args, **kwargs)
  File "C:\Users\Aymeric\Python33\lib\site-packages\praw\__init__.py", line 483, in request_json
    data = json.loads(response, object_hook=hook)
  File "C:\Users\Aymeric\Python33\lib\json\__init__.py", line 332, in loads
    return cls(**kw).decode(s)
  File "C:\Users\Aymeric\Python33\lib\json\decoder.py", line 352, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Aymeric\Python33\lib\json\decoder.py", line 370, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Edit2:如果我删除?context = 3 并将列表更改为字符串,它现在正在运行。显然,它从来没有作为一个列表工作(我的坏记忆在那里!),?context = 3 似乎是问题。

1 个答案:

答案 0 :(得分:1)

你是对的?context=3,答案很简单,PRAW在你的问题时不支持url参数。现在使用PRAW 2.1.13或更高版本。使用pip install praw -U升级您的PRAW版本。

您认为get_submission将列表作为参数,可能来自于将其与get_submissions方法混淆,后者确实将迭代作为参数。