Error:urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:xxx)>
Solution:
Add the following codes before you use urllib.request.Request(url)
:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Problem analysis
This is because the website visited is HTTPS://
, which requires SSL authentication, and using urllib
directly will lead to local authentication failure (the specific reason is not found out), so SSL is used_create_unverified_Context
turn off authentication
Error recurrence
When request = urllib.Request.Request (URL, data)
is executed, an error is reported. Cancel the comments in the upper two lines to solve the problem
import json
import urllib
def baidu_search():
url = "https://www.baidu.com/s?"
data = {"wd": "AHA"}
data = json.dumps(data).encode('GBK')
# import ssl
# ssl._create_default_https_context = ssl._create_unverified_context # If these two lines are not added, the next line reports an error
request = urllib.request.Request(url, data)
response = urllib.request.urlopen(request)
content = response.read()
print(str(content))
if __name__ == '__main__':
baidu_search()
Read More:
- [Solved] urllib.error.URLError: <urlopen error [SSL: WRONG_VERSION_NUMBER] wrong version number
- [Solved] urllib.error.URLError: urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]
- [Solved] Pytorch Download CIFAR1 Datas Error: urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certi
- Error when downloading the built-in dataset of pytoch = urllib.error.urlerror: urlopen error [SSL: certificate_verify_failed]
- urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>
- Python 3 urllib has no URLEncode attribute
- [Solved] raise ContentTooShortError(urllib.error.ContentTooShortError: <urlopen error retrieval incomplete:
- SSL error of urllib3 when Python uploads files using Minio
- Python error: urllib.error.HTTPError : http Error 404: not found
- When sending HTTP request, python encountered: error 54, ‘connection reset by peer’ solution
- urllib.error.HTTPError: HTTP Error 403: Forbidden [How to Solve]
- [Solved]AttributeError: module ‘urllib’ has no attribute ‘quote’
- urllib.error.HTTPError http error 403 forbidden
- urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host=‘localhost‘, port=8097): Max retries excee
- Python raspberry pie starts sending IP address to mailbox
- Using Python error urlopen error unknown URL type: the solution of HTTPS
- python chatterbot [nltk_data] Error loading stopwords: <urlopen error [Errno 11004]
- [Solved] Request.url is not modifiable, use Request.replace() instead
- python3 ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833) Error