[英]SSL: CERTIFICATE_VERIFY_FAILED error on windows
I'm building a simple program to go through a list of URLs and extract their content using beautiful soup. 我正在构建一个简单的程序,以浏览URL列表并使用漂亮的汤提取其内容。 For the minute I'm just trying to iterate through the list and retrieve the html but I keep getting the following error:
目前,我只是尝试遍历列表并检索html,但我不断收到以下错误:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 964, in send
self.connect()
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1400, in connect
server_hostname=server_hostname)
File "C:\ProgramData\Anaconda3\lib\ssl.py", line 401, in wrap_socket
_context=self, _session=session)
File "C:\ProgramData\Anaconda3\lib\ssl.py", line 808, in __init__
self.do_handshake()
File "C:\ProgramData\Anaconda3\lib\ssl.py", line 1061, in do_handshake
self._sslobj.do_handshake()
File "C:\ProgramData\Anaconda3\lib\ssl.py", line 683, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/thoma/PycharmProjects/fyp/urls_and_prep/parsing_html.py", line 17, in <module>
response = urllib.request.urlopen(req)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 544, in _open
'_open', req)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1361, in https_open
context=self._context, check_hostname=self._check_hostname)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1320, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>
My program is simple enough but I can't understand or find any good resources for working out what exactly is going on/ how to handle it. 我的程序很简单,但是我无法理解或找到任何好的资源来弄清楚到底发生了什么/如何处理它。 I know its something to do with SSL certificates but I'm not sure where how to use them or where to install them etc. I'm just at a bit of a loss with this one as I've never really worked with SSL before.
我知道它与SSL证书有关,但是我不确定在哪里使用它们或在哪里安装它们等等。我对此感到有点茫然,因为我以前从未真正使用过SSL 。 Any guidance or help is greatly appreciated.
任何指导或帮助,我们将不胜感激。 Here's the code:
这是代码:
import urllib.request
from bs4 import BeautifulSoup
file = open("all_urls.txt", "r")
for line in file:
print(line)
try:
response = urllib.request.urlopen(line)
html = response.read()
except ValueError:
print(ValueError)
continue
soup = BeautifulSoup(html, 'lxml')
print(soup.get_text())
Are you using Windows or Linux? 您使用Windows还是Linux? This problem seems to be not on Python, but in Anaconda or in the OS.
这个问题似乎不是在Python上,而是在Anaconda或OS中。 You can try some simple solutions, like: 1 - Execute the scrypt using other Python installation than the one from Anaconda.
您可以尝试一些简单的解决方案,例如:1-使用除Anaconda之外的其他Python安装执行scrypt。 2 - Use the virtualenv to isolate the components of the OS.
2-使用virtualenv隔离操作系统的组件。
Below will fix the problem. 下面将解决问题。 But be sure not to use in production as it will be working without verifying the SSL certificate-
但是请确保不要在生产环境中使用,因为它无需验证SSL证书即可正常工作-
import urllib
from bs4 import BeautifulSoup
import ssl
# This is a temporary fix .Be carefule of malicious links
context = ssl._create_unverified_context()
file = open("all_urls.txt", "r")
for line in file:
print(line)
try:
response = urllib.request.urlopen(line, context=context)
html = response.read()
except ValueError:
print(ValueError)
continue
soup = BeautifulSoup(html, 'lxml')
print(soup.get_text())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.