前提・実現したいこと
Jupyter labを使ってウェブスクレイピングをしており、HTMLデータを取得しようとしております。
しかし、次のエラーが出ました。
Jupyterlab
1URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1123)>
そこで、下記サイトを参考に、
https://qiita.com/orangain/items/0a641d980019fd7e0c52
certifiモジュールで、ルート証明書を取得しようとしたのですが、エラーになってしまいます。
発生している問題・エラーメッセージ
Jupyterlab
1--------------------------------------------------------------------------- 2SSLCertVerificationError Traceback (most recent call last) 3/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py in do_open(self, http_class, req, **http_conn_args) 4 1345 try: 5-> 1346 h.request(req.get_method(), req.selector, req.data, headers, 6 1347 encode_chunked=req.has_header('Transfer-encoding')) 7 8/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py in request(self, method, url, body, headers, encode_chunked) 9 1254 """Send a complete request to the server.""" 10-> 1255 self._send_request(method, url, body, headers, encode_chunked) 11 1256 12 13/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py in _send_request(self, method, url, body, headers, encode_chunked) 14 1300 body = _encode(body, 'body') 15-> 1301 self.endheaders(body, encode_chunked=encode_chunked) 16 1302 17 18/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py in endheaders(self, message_body, encode_chunked) 19 1249 raise CannotSendHeader() 20-> 1250 self._send_output(message_body, encode_chunked=encode_chunked) 21 1251 22 23/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py in _send_output(self, message_body, encode_chunked) 24 1009 del self._buffer[:] 25-> 1010 self.send(msg) 26 1011 27 28/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py in send(self, data) 29 949 if self.auto_open: 30--> 950 self.connect() 31 951 else: 32 33/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py in connect(self) 34 1423 35-> 1424 self.sock = self._context.wrap_socket(self.sock, 36 1425 server_hostname=server_hostname) 37 38/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session) 39 499 # ctx._wrap_socket() 40--> 500 return self.sslsocket_class._create( 41 501 sock=sock, 42 43/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session) 44 1039 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets") 45-> 1040 self.do_handshake() 46 1041 except (OSError, ValueError): 47 48/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py in do_handshake(self, block) 49 1308 self.settimeout(None) 50-> 1309 self._sslobj.do_handshake() 51 1310 finally: 52 53SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1123) 54 55During handling of the above exception, another exception occurred: 56 57URLError Traceback (most recent call last) 58<ipython-input-7-81afa538059d> in <module> 59 1 url = 'https://scraping-for-beginner.herokuapp.com/ranking/' 60----> 2 response = req.urlopen(url) # 指定したwebサイトのHTMLを取得できる関数 61 62/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 63 212 else: 64 213 opener = _opener 65--> 214 return opener.open(url, data, timeout) 66 215 67 216 def install_opener(opener): 68 69/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py in open(self, fullurl, data, timeout) 70 515 71 516 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method()) 72--> 517 response = self._open(req, data) 73 518 74 519 # post-process response 75 76/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py in _open(self, req, data) 77 532 78 533 protocol = req.type 79--> 534 result = self._call_chain(self.handle_open, protocol, protocol + 80 535 '_open', req) 81 536 if result: 82 83/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args) 84 492 for handler in handlers: 85 493 func = getattr(handler, meth_name) 86--> 494 result = func(*args) 87 495 if result is not None: 88 496 return result 89 90/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py in https_open(self, req) 91 1387 92 1388 def https_open(self, req): 93-> 1389 return self.do_open(http.client.HTTPSConnection, req, 94 1390 context=self._context, check_hostname=self._check_hostname) 95 1391 96 97/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py in do_open(self, http_class, req, **http_conn_args) 98 1347 encode_chunked=req.has_header('Transfer-encoding')) 99 1348 except OSError as err: # timeout error 100-> 1349 raise URLError(err) 101 1350 r = h.getresponse() 102 1351 except: 103 104URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1123)>
terminal
1-bash: /Applications/Python 3.9/Install Certificates.command: No such file or directory
該当のソースコード
Jupyterlab
1!pip3 install beautifulsoup4 2 3from bs4 import BeautifulSoup 4import urllib.request as req 5 6url = 'https://scraping-for-beginner.herokuapp.com/ranking/' 7response = req.urlopen(url)
certifiモジュールの方は、何を変えればよいかわからず、とりあえずバージョンの数字だけ自分のpyhtonのバージョンに合わせました
terminal
1MacBook-Air:~ a$ /Applications/Python\ 3.9/Install\ Certificates.command
試したこと
自分のpythonが格納されているパスでないとダメなのかと思い、
一度pythonをアップグレードしたところ、
terminal
1They will install into the site-package directory 2 /usr/local/lib/python3.9/site-packages
と出たので、
terminal
1$ /usr/local/Cellar/python@3.9/Install\ Certificates.command 2や、 3$ /usr/local/bin/Python\ 3.9/Install\ Certificates.command
なども入力してみましたが、全て同じエラーです
補足情報(FW/ツールのバージョンなど)
OS:Mac、Pythonのバージョン:3.9.2_1
回答2件
あなたの回答
tips
プレビュー