gpt4 book ai didi

python - PRAW:get_submission 工作不一致

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:56 24 4
gpt4 key购买 nike

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

最佳答案

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

您认为 get_submission 将列表作为参数,可能是因为将它与 get_submissions 混淆了方法,它确实将可迭代对象作为参数。

关于python - PRAW:get_submission 工作不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20890505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com