スポンサーリンク
webスクレイピングを試していた際に、SSL証明書検証エラー(SSLCertVerificationError)に躓いたので同じ事象でお困りの方の参考になればと思い、対処方法を残しておこうと思います。
Contents
webスクレイピング時に起きたSSL証明書検証エラー(SSLCertVerificationError)
ソースコード(BeautifulSoup)
# ライブラリの読み込み
from urllib import request
from bs4 import BeautifulSoup
# URL
url = "https://lusknote.com/"
# URLにアクセス
html = request.urlopen(url)
# HTMLをBeautifulSoupで扱う
soup = BeautifulSoup(html, "html.parser")
title = soup.find('title').get_text()
# 出力する
print(title)
エラー内容(SSLCertVerificationError)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1346, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1253, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1299, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1248, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1008, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 948, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1422, in connect
self.sock = self._context.wrap_socket(self.sock,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)
原因
Pythonをインストール直後の状態ではルート証明書が含まれていないようです。
そのため、初期状態ではサーバー証明書の検証に失敗するようになっています。
対処方法
下記コマンドを実行し、証明書をインストールする。
/Applications/Python\ 3.9/Install\ Certificates.command
実行結果
-- pip install --upgrade certifi
Collecting certifi
Using cached certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
Installing collected packages: certifi
Successfully installed certifi-2021.5.30
WARNING: You are using pip version 21.1.1; however, version 21.1.3 is available.
You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -m pip install --upgrade pip' command.
-- removing any existing file or link
-- creating symlink to certifi certificate bundle
-- setting permissions
-- update complete
これでhttps通信を行えるようになりました!
参考にしたサイト
まとめ
本記事では、Python環境をインストール直後にサーバー証明書の検証に失敗する事例についてまとめてみました。少しでも参考になっておりましたら幸いです。
スポンサーリンク