该脚本从 Reddit 上的特定评论中获取信息。它是否有效取决于给它的永久链接。
import praw
from pprint import pprint
clipboard = ['permalink']
com = r.get_submission(clipboard).comments[0]
pprint(vars(com))
我觉得以 ?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似乎是问题所在。