SSL 认证验证失败

SSL Certification verify failed

这几天我一直在努力学习 python,但我 运行 遇到了一个我不太确定如何解决的问题。我正在尝试制作一个简单的 reddit 机器人并学习 praw reddit API。当我 运行 以下机器人时:

import praw
import time

r = praw.Reddit('testmachine11968986531')
test = r.submission(id="5u7q8x")

comment_user = set()   # to avoid duplicates

for i in xrange(0,10):  # Run the loop 10 times
    #comments = r.comments(submission)
    for comment in test.comments:
        body = comment.body.lower()
        if body.find("think") != -1 or body.find("please") != -1:
            comment_user.add(comment.author)
    #time.sleep(120)   # Sleep for 2 minutes

print "Here are some comments:"
for user in polite_users:
    print user

我收到一个错误:

RequestException: error with request [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

我四处寻找,看到我可以插入类似

的内容
verify = False

在某种 get() 实例中,但我不确定这是否适用于这个特定示例。我相信其他一切都很好——我可以很好地使用 pip,等等

如有任何帮助,我们将不胜感激。万分感谢。

编辑:完整的错误回溯是

 Traceback (most recent call last):
  File "C:\Users\**\Desktop\Bottest\startBot.py", line 16, in <module>
    for comment in test.comments:
  File "C:\Python27\lib\site-packages\praw\models\reddit\base.py", line 31, in __getattr__
    self._fetch()
  File "C:\Python27\lib\site-packages\praw\models\reddit\submission.py", line 133, in _fetch
    'sort': self.comment_sort})
  File "C:\Python27\lib\site-packages\praw\reddit.py", line 320, in get
    data = self.request('GET', path, params=params)
  File "C:\Python27\lib\site-packages\praw\reddit.py", line 404, in request
    params=params)
  File "C:\Python27\lib\site-packages\prawcore\sessions.py", line 133, in request
    self._authorizer.refresh()
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 328, in refresh
    password=self._password)
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 138, in _request_token
    response = self._authenticator._post(url, **data)
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 29, in _post
    data=sorted(data.items()))
  File "C:\Python27\lib\site-packages\prawcore\requestor.py", line 48, in request
    raise RequestException(exc, args, kwargs)
RequestException: error with request [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

您的问题可能出在您的 user_agent 中。 Reddit 需要一个唯一的 user_agent。你 user_agent 是你唯一标识你的脚本的方式。 Reddit API 维基页面 (https://github.com/reddit/reddit/wiki/API) 有关于 user_agent 字符串和其他所有内容的官方和更新建议。强烈推荐阅读。

除此之外,您的 user_agent 字符串必须 永远不会 包含字符串 bot。这意味着你应该改变你的线路:

r = praw.Reddit('bot1')

像这样:

r = praw.Reddit('Name of your bot [version] by [Your Reddit username or your name]')

[你可能想阅读这个关于这个问题的优秀教程 https://praw.readthedocs.io/en/v3.6.0/pages/getting_started.html#connecting-to-reddit]

否则你可以在创建 praw 对象后添加:

r.config._ssl_url = None

不推荐,因为这意味着您的所有信息都可能被中间人拦截

您还可以:

添加:

r.login('bot_username', 'bot_password')

r = praw.reddit([...])

之后

test = r.submission(id="5u7q8x") 更改为 test = r.get_submission(submission_id='5u7q8x')

并在之后添加:

test = praw.helpers.flatten_tree(test.comments)

您现在应该能够遍历 test 而不是 test.comments

如果这不起作用,我不知道还有什么可以。