簡體   English   中英

python 請求:(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] 錯誤的版本號 (_ssl.c:1123)'))

[英]python requests: (SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)'))

我有一個 python 腳本,它可以從籃球參考中獲取一些信息。com

由於此錯誤,它今天停止工作:


  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
    conn.connect()

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connection.py", line 362, in connect
    self.sock = ssl_wrap_socket(

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 386, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)

  File "C:\Users\pphotsauce\anaconda3\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(

  File "C:\Users\pphotsauce\anaconda3\lib\ssl.py", line 1040, in _create
    self.do_handshake()

  File "C:\Users\pphotsauce\anaconda3\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()

SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\adapters.py", line 439, in send

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 726, in urlopen
    retries = retries.increment(

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\util\retry.py", line 446, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))

MaxRetryError: HTTPSConnectionPool(host='www.basketball-reference.com', port=443): Max retries exceeded with url: /search/search.fcgi?search=RJ+Barrett (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')))


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "G:\fan_duel\april_version\main_2.py", line 13, in <module>
    test = [f.get_bref_id_tester(x) for x in full_names]

  File "G:\fan_duel\april_version\main_2.py", line 13, in <listcomp>
    test = [f.get_bref_id_tester(x) for x in full_names]

  File "G:\fan_duel\april_version\functions.py", line 30, in get_bref_id_tester
    search_results_page = requests.get(url, headers=headers, allow_redirects='False')

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\api.py", line 76, in get

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\api.py", line 61, in request

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\sessions.py", line 542, in request

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\sessions.py", line 655, in send

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\adapters.py", line 514, in send

SSLError: HTTPSConnectionPool(host='www.basketball-reference.com', port=443): Max retries exceeded with url: /search/search.fcgi?search=RJ+Barrett (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')))


runfile('G:/fan_duel/april_version/main_2.py', wdir='G:/fan_duel/april_version')
Reloaded modules: functions
Traceback (most recent call last):

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
    conn.connect()

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connection.py", line 362, in connect
    self.sock = ssl_wrap_socket(

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 386, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)

  File "C:\Users\pphotsauce\anaconda3\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(

  File "C:\Users\pphotsauce\anaconda3\lib\ssl.py", line 1040, in _create
    self.do_handshake()

  File "C:\Users\pphotsauce\anaconda3\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()

SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\adapters.py", line 439, in send

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\connectionpool.py", line 726, in urlopen
    retries = retries.increment(

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\urllib3\util\retry.py", line 446, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))

MaxRetryError: HTTPSConnectionPool(host='www.basketball-reference.com', port=443): Max retries exceeded with url: /search/search.fcgi?search=Chris+Boucher (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')))


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "G:\fan_duel\april_version\main_2.py", line 13, in <module>
    test = [f.get_bref_id_tester(x) for x in full_names]

  File "G:\fan_duel\april_version\main_2.py", line 13, in <listcomp>
    test = [f.get_bref_id_tester(x) for x in full_names]

  File "G:\fan_duel\april_version\functions.py", line 33, in get_bref_id_tester
    search_results_page = requests.get(url, headers=headers, allow_redirects='False')

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\api.py", line 76, in get

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\api.py", line 61, in request

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\sessions.py", line 542, in request

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\sessions.py", line 655, in send

  File "C:\Users\pphotsauce\anaconda3\lib\site-packages\requests\adapters.py", line 514, in send

SSLError: HTTPSConnectionPool(host='www.basketball-reference.com', port=443): Max retries exceeded with url: /search/search.fcgi?search=Chris+Boucher (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')))

我有一個使用 NBA 球員全名並返回籃球參考 ID 的 function。 如果我將此 function 應用於 pandas 系列或遍歷名稱列表,則只有部分名稱(每次不同的名稱)會導致錯誤。 ('nightmare' 是一本名字很麻煩的字典)

nightmare = {
    
'Bol Bol': 'bolbo01',
'Nicolo Melli': 'mellini01',
'Davis Bertans': 'bertada01',
'Tomas Satoransky': 'satorto01',
'Theo Maledon': 'maledth01',
'Bogdan Bogdanovic': 'bogdabo01',

}

def get_bref_id(full_name):
    sleep(0.2)
    if full_name not in nightmare:
        try:
            first_name, last_name = full_name.split(' ')
            url = 'https://www.basketball-reference.com/search/search.fcgi?search='+first_name+'+'+last_name
            search_results_page = requests.get(url, allow_redirects = "False")
            soup = bs(search_results_page.content, 'html.parser')
            potential_links = soup.find_all('div', class_="search-item-name")
            bucket = [element for element in potential_links if '202' in element.text if first_name in element.text]
            bref_id = str(bucket[0]).replace('.', '/').split('/')[3]
        except IndexError: # b-ref search occasionally takes you straight to the page
            bref_id = search_results_page.url.split('/')[5].split('.')[0]
        finally:
            return bref_id
    else:
        return nightmare[full_name]

我對 SSL 或可能導致此問題的原因一無所知。 如果您能指出正確的方向以了解更多信息,我將不勝感激。

服務器www.basketball-reference.com至少需要 TLS 1.2。 看起來您的 Python 與 OpenSSL 的版本相關聯,該版本太舊而無法支持 TLS 1.2。 使用以下代碼檢查使用的是哪個 OpenSSL 版本。 很久以前,OpenSSL 1.0.1 中添加了對 TLS 1.2 的支持,但例如 MacOS 很長一段時間都與舊版本 OpenSSL 0.9.8 一起發布。

import ssl
print(ssl.OPENSSL_VERSION)

嘗試在 headers 參數中添加用戶代理。 此外,如果搜索返回超過 1 個選項,我添加了一個小輸入選項,因此您可以選擇:

代碼:

from bs4 import BeautifulSoup as bs
import requests
from time import sleep

#pip install choice
import choice

nightmare = {
    
'Bol Bol': 'bolbo01',
'Nicolo Melli': 'mellini01',
'Davis Bertans': 'bertada01',
'Tomas Satoransky': 'satorto01',
'Theo Maledon': 'maledth01',
'Bogdan Bogdanovic': 'bogdabo01',

}

def get_bref_id(full_name):
    sleep(0.2)
    if full_name not in nightmare:
        first_name, last_name = full_name.split(' ')
        url = 'https://www.basketball-reference.com/search/search.fcgi?search='+first_name+'+'+last_name
        headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'}
        search_results_page = requests.get(url, headers=headers, allow_redirects = "False")
        soup = bs(search_results_page.content, 'html.parser')
        potential_links = soup.find_all('div', class_="search-item-name")
        
        if len(potential_links) > 1:
            bucket = {}
            for element in potential_links:
                player = element.find('a').text
                playerId = element.find('a')['href'].split('/')[-1].replace('.html','')
                statsType = element.find('a')['href'].split('/')[1]
                bucket[player + ' (%s)' %statsType] = playerId
            print('Which player id did you want?')
            player_choice = choice.Menu(bucket.keys()).ask()
            bref_id = bucket[player_choice]
        
        else:
            try:
                element = soup.find_all('div', class_="search-item-name")[0]
                bref_id = element.find('a')['href'].split('/')[-1].replace('.html','')
            except IndexError: # b-ref search occasionally takes you straight to the page
                bref_id = search_results_page.url.split('/')[5].split('.')[0]
            
        return bref_id
    else:
        return nightmare[full_name]

Output:

Which player id did you want?
Make a choice:
 0: Michael Jordan (1985-2003) (players)
 1: Michael Jordan (1984-1992) (international)
 2: Michael-Hakim Jordan (2006-2009) (international)
 3: Michael Jordan (2000-2003) (executives)

Enter number or name; return for next page

? 0

jordami01

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM