Fix SSL Certificate Verify Failed Error in Python3
If you're using Python3 and running into an SSL Certificate Verify Failed error, don't worry - it's a common issue that can be easily fixed. This error occurs when Python fails to verify the SSL certificate of the website you're trying to connect to.
To fix this issue, you can add a few lines of code before making the request. First, import the ssl library using
import ssl. Then, create a new SSL context using
context = ssl.create_default_context(). Finally, set the SSL context to not verify the certificates using
context.check_hostname = False and
context.verify_mode = ssl.CERT_NONE.
Here's an example of putting it all together:
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
response = urllib.request.urlopen(url, context=context)
By adding these lines of code, you should be able to successfully make the request without encountering the SSL Certificate Verify Failed error.