Python Scraping Error – SSL: CERTIFICATE_VERIFY_FAILED

Python Scraping Error – SSL: CERTIFICATE_VERIFY_FAILED is an error which occurs in Python when the site you are trying to access has no ssl certificate.

In this Blog Post I am going to show you why this error is happening after trying to scrape data for example and how you can solve it.

I am glad to also share with you other possible solutions which have worked for me and other developers who encountered the error.

Explaining Python Scraping Error – SSL: CERTIFICATE_VERIFY_FAILED

First of all, we need to understand when the error happens in the first place, in most cases the error occurs when you are using some scrpaing package or solution, for example with Selenium or Scrapy.

Here is an example of the error message.

                                                                       #
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1049)>
                                                                       #

Bellow I provide you with some solutions which worked for my particular case. Please make sure to not give up and try each one before you lose hope, sometimes even the last solution can work.

Solution 1 : Cmd Command

The first solution is mainly for people with windows machines but this can also work for Linux and mac users.

Open your terminla aka CMD, you can try to open it as an admin. Then run the following command.

                                                                       #
open /Applications/Python\ 3.7/Install\ Certificates.command
                                                                       #

This may also work for mac and Linux as this command is for python and is not related to a specific operating system.

Solution 2 : install the Python certificates

In the second solution, we focus on Mac users as this error happens usually in Mac and is a very popular error. The solution in his case is to install the Python certificates.

Step by Step, try running the first script followed by the second one.

But, first you need to find them

You should go to the application folder.

The navigate to the Python folder, inside the folder you should find, two scripts that you can run or double click, for people who like running it, Start with

                                                                       #
Install Certificates.command
                                                                       #

The add and run the line bellow

                                                                       #
Update Shell Profile.command
                                                                       #

The error should be gone forever, if that did not work, try the solution bellow.

Solution 3 : using an unverified certificate

Another solution is to run one command, but there is a twist.

You should only run the command if you are ok with the consequences of using unverified ssl

Just understand that by using an unverified certificate you are putting your self/app at risk.

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

After running the command bellow, the error should be gone.

                                                                       #

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
                                                                       #

If you loved this post, stay around, we have a lot of other fixes for other problems related to Python, by the way you can donate using the big red Koffi button above, thank you.

Summing-up

This is the end of our article, I hope this helped you solve the error SSL: CERTIFICATE_VERIFY_FAILED or at least pointed you to the right direction. You can donate to our Kofi account but its not necessary.

Keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/