[python] ssl.SSLCertVerificationError

1. Error message

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1007)

C:\Users\\
a.chen\AppData\Local\Microsoft\WindowsApps\python3.10.exe C:\Users\\
a.chen\PycharmProjects\python_for_work\ auto_click\for_jenkins_CN\build_now.py
Traceback (most recent call last):
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \connectionpool.py", line 1042, in _validate_conn
    conn. connect()
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \connection.py", line 414, in connect
    self.sock = ssl_wrap_socket(
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \util\ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \util\ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\ssl.py", line 1071, in _create
    self. do_handshake()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\ssl.py", line 1342, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1007)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\ \adapters.py", line 489, in send
    resp = conn.urlopen(
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \connectionpool.py", line 787, in urlopen
    retries = retries. increment(
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\ \util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='blog.csdn.net', port=443): Max retries exceeded with url: /cun_king (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1007)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\\
a.chen\PycharmProjects\python_for_work\auto_click\for_jenkins_CN\build_now.py", line 21, in <module>
    strhtml = requests.get(url=url, headers=headers) # Get method to get web page data
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\ \api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\ \api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\ \sessions.py", line 587, in request
    resp = self. send(prep, **send_kwargs)
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\ \sessions.py", line 701, in send
    r = adapter. send(request, **kwargs)
  File "C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\ \adapters.py", line 563, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='blog.csdn.net', port=443): Max retries exceeded with url: /cun_king (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1007)')))

Process finished with exit code 1

2. Solution

Add verify=False to the requests.get() function, so that the client will not verify SSL;

3. Correct executable code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 5/8/2023 3:32 PM
# @Author : Nora
# @File : build_now.py
# @Description : to show hits;


import requests
from bs4 import BeautifulSoup
import ssl
import urllib


url = 'https://blog.csdn.net/cun_king'
# context = ssl._create_unverified_context()
# data = urllib.request.urlopen(url, context=context).read().decode('utf-8')

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                         'Chrome/112.0.0.0 Safari/537.36'}
strhtml = requests.get(url=url, headers=headers,verify=False) #Get method to get web page data
soup = BeautifulSoup(strhtml. text, 'lxml')
info = soup.select('#userSkin > div.user-profile-head > '
                   'div.user-profile-head-info > div.user-profile-head-info-t > div > div.user-profile-head-info-rr > div.user-profile-head-info-r-c > ul > '
                   'li:nth-child(1) > span > span > span > div. user-profile-statistics-num')
print("Visit volume:" + info[0].get_text())

4. Result display:

C:\Users\\
a.chen\AppData\Local\Microsoft\WindowsApps\python3.10.exe C:\Users\\
a.chen\PycharmProjects\python_for_work\ auto_click\for_jenkins_CN\build_now.py
C:\Users\\
a.chen\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool. py:1045: InsecureRequestWarning: Unverified HTTPS request is being made to host 'blog.csdn.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced - usage.html#ssl-warnings
  warnings. warn(
Visits: 144,796

Process finished with exit code 0