2

该脚本从 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似乎是问题所在。

4

1 回答 1

1

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

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

于 2014-03-06T20:26:38.680 回答