我的代码目前是:
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import requests
import praw
import time
import re
import bs4
USERAGENT = 'web:PeskyTrollBot:v0.1 (by /u/redacted)'
PASSWORD = 'NOTREALLYMYPASSWORD'
USERNAME = "PeskyTrollBot"
ID = 'redacted'
SECRET = 'redacted'
def authenitcate():
print("Authenticating...")
reddit = praw.Reddit(client_id=ID, client_secret=SECRET,
password=PASSWORD, user_agent=USERAGENT,
username=USERNAME)
print("Authenticated as {}\n" .format(reddit.user.me()))
return reddit
authenitcate()
和我的控制台输出(现在安装了pyopenssl之后)如下:
C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py
Authenticating...
Traceback (most recent call last):
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 441, in wrap_socket
cnx.do_handshake()
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1716, in do_handshake
self._raise_ssl_error(self._ssl, result)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1456, in _raise_ssl_error
_raise_current_error()
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 595, in urlopen
self._prepare_proxy(conn)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 816, in _prepare_proxy
conn.connect()
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 326, in connect
ssl_context=context)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 448, in wrap_socket
raise ssl.SSLError('bad handshake: %r' % e)
ssl.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 440, in send
timeout=timeout
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\retry.py", line 388, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\requestor.py", line 47, in request
return self._http.request(*args, timeout=TIMEOUT, **kwargs)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 506, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py", line 19, in <module>
authenticate()
File "C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py", line 16, in authenticate
print("Authenticated as {}\n" .format(reddit.user.me()))
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\praw\models\user.py", line 60, in me
user_data = self._reddit.get(API_PATH['me'])
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\praw\reddit.py", line 367, in get
data = self.request('GET', path, params=params)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\praw\reddit.py", line 472, in request
params=params)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 175, in request
params=params, url=url)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 121, in _request_with_retries
retries, saved_exception, url)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 87, in _do_retry
params=params, url=url, retries=retries - 1)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 121, in _request_with_retries
retries, saved_exception, url)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 87, in _do_retry
params=params, url=url, retries=retries - 1)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 109, in _request_with_retries
data, files, json, method, params, retries, url)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 94, in _make_request
params=params)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\rate_limit.py", line 32, in call
kwargs['headers'] = set_header_callback()
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 135, in _set_header_callback
self._authorizer.refresh()
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 328, in refresh
password=self._password)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 138, in _request_token
response = self._authenticator._post(url, **data)
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 29, in _post
data=sorted(data.items()))
File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\requestor.py", line 49, in request
raise RequestException(exc, args, kwargs)
prawcore.exceptions.RequestException: error with request HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))
Process finished with exit code 1
我已尝试过其他stackoverflow帖子的多个建议,例如更新我的useragent以符合Reddit准则。我已经尝试告诉它在我创建praw对象后不立即验证ssl证书并且错误仍然存在。我也尝试过使用try catch循环,每次捕获后都有一分钟睡眠无效。任何疑难解答的想法?我已经用完了其他帖子的提示,所以我现在正在制作自己的提示。