Python Forum
"SSL: CERTIFICATE_VERIFY_FAILED” error on Python 3.9.6 (Windows 10)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"SSL: CERTIFICATE_VERIFY_FAILED” error on Python 3.9.6 (Windows 10)
#1
Hi!
I’m new with Python and I have been following some tutorials and based on one of them I have the following piece of code:

from scrapy import Selector
from urllib.request import urlopen

html = urlopen("https://www.pythonparatodos.com.br/formulario.html")
sel = Selector(text = html.read())
lista = sel.xpath('//input[@type="text"]')
print(lista)
for selector in lista:
    print(selector)
Thanks in advance!

The URL is easily accessed through a browser but when I run the code I got the following errors:


Error:
C:\Users\username\Envs\webcrawler_part_ii\Scripts\python.exe C:/Users/username/PycharmProjects/webcrawler_part_ii/aula44_scrapy_lxml_exemplos.py Traceback (most recent call last): File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1346, in do_open h.request(req.get_method(), req.selector, req.data, headers, File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1257, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1303, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1252, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1012, in _send_output self.send(msg) File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 952, in send self.connect() File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1426, in connect self.sock = self._context.wrap_socket(self.sock, File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1040, in _create self.do_handshake() File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\username\PycharmProjects\webcrawler_part_ii\aula44_scrapy_lxml_exemplos.py", line 5, in <module> html = urlopen("https://www.pythonparatodos.com.br/formulario.html") File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen return opener.open(url, data, timeout) File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 517, in open response = self._open(req, data) File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 534, in _open result = self._call_chain(self.handle_open, protocol, protocol + File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain result = func(*args) File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1389, in https_open return self.do_open(http.client.HTTPSConnection, req, File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1349, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1129)> Process finished with exit code 1
I’ve found some possible solutions on google to this with certifi or with the two lines of code below but none of them worked. Can someone help me on this?

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
Reply
#2
Use Requests and not urllib.
Site also need a User-agent or will not give access.
from scrapy import Selector
import requests

user_agent = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36'}
html = requests.get("https://www.pythonparatodos.com.br/formulario.html", headers=user_agent)
sel = Selector(text = html.content)
lista = sel.xpath('//input[@type="text"]')
#print(lista)
for selector in lista:
    print(selector)
Output:
<Selector xpath='//input[@type="text"]' data='<input type="text" name="nome" size="...'> <Selector xpath='//input[@type="text"]' data='<input type="text" name="email" size=...'> <Selector xpath='//input[@type="text"]' data='<input type="text" name="celular" siz...'>
Reply
#3
Hi All,
I try to post a question but can not find the icon to do it.
So I drop here and hopefully someone shows me where it is.
Thanks!
Reply
#4
(May-09-2022, 10:31 PM)duonght Wrote: I try to post a question but can not find the icon to do it.
Up in right corner there is button Post Thread.
Click on it and write your question-
Reply
#5
(May-09-2022, 02:58 PM)snippsat Wrote: Use Requests and not urllib.
Site also need a User-agent or will not give access.
from scrapy import Selector
import requests

user_agent = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36'}
html = requests.get("https://www.pythonparatodos.com.br/formulario.html", headers=user_agent)
sel = Selector(text = html.content)
lista = sel.xpath('//input[@type="text"]')
#print(lista)
for selector in lista:
    print(selector)
Output:
<Selector xpath='//input[@type="text"]' data='<input type="text" name="nome" size="...'> <Selector xpath='//input[@type="text"]' data='<input type="text" name="email" size=...'> <Selector xpath='//input[@type="text"]' data='<input type="text" name="celular" siz...'>

Thank you! It works.
Out of curiosity, is there any specific reason for the failure with urllib?
likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to get around SSL: CERTIFICATE_VERIFY_FAILED sawtooth500 3 173 Apr-16-2024, 01:28 PM
Last Post: sawtooth500
  error while installing any library using pip in windows AkashKansal 1 4,390 Sep-24-2020, 07:51 AM
Last Post: buran
  Correct py got error on another windows 7 meetinnet 8 3,844 Apr-25-2020, 04:57 PM
Last Post: meetinnet
  Python 3.7, Windows 7, Syntax Error hughdent 1 2,273 Mar-23-2020, 10:09 AM
Last Post: buran
  Error in compiling cython extension on Python 3.6.4 on Windows 8 goldenmean 3 5,757 Jun-05-2019, 09:37 PM
Last Post: Larz60+
  Windows 10 Task Scheduler Error mypython 1 2,484 Aug-11-2018, 11:01 PM
Last Post: ichabod801
  Windows cannot find "wish.exe" - error while trying to run PAGE Nwb 0 7,525 Jun-11-2018, 12:08 PM
Last Post: Nwb
  MechanicalSoup - SSL: CERTIFICATE_VERIFY_FAILED behind proxy 5u88u 0 3,958 Jun-08-2018, 01:51 PM
Last Post: 5u88u
  Error when creating child process on Windows 10 Charles 0 3,229 May-30-2018, 01:24 PM
Last Post: Charles
  Windows server 2008 SP2 - Python cx_Oracle connection DLL error tpanagoda 2 5,043 Mar-05-2018, 04:35 AM
Last Post: tpanagoda

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020