BeautifulSoup4を利用したスクレイピングをテストしています。
以下ソース(独学プログラマーという本のサンプルです)
Python
1import urllib.request 2from bs4 import BeautifulSoup 3 4class Scraper: 5 def __init__(self, site): 6 self.site = site 7 8 def scrape(self): 9 r = urllib.request\ 10 .urlopen(self.site) #ここでエラーが発生している。 11 html = r.read() 12 parser = "html.parser" 13 sp = BeautifulSoup(html, 14 parser) 15 for tag in sp.find_all("a"): 16 url = tag.get("href") 17 if url is None: 18 continue 19 if "html" in url: 20 print("\n" + url) 21 22news = "https://news.google.com/" 23Scraper(news).scrape()
上記を実行すると下記のエラーが出るのですが、原因がわかりません。
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1342, 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 1255, 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 1301, 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 1250, 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 1010, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 950, in send self.connect() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1424, 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:1122)
サンプルプログラム自体が動作しないので、環境の問題かとも思うのですが原因がわかりません。
原因及び解決法をご教示頂けると助かります。
試したこと
Pythonの再インストール
BeautifulSoup4の再インストール
環境情報
Pythonバージョン:3.9
MacOS:10.15.7
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/26 12:27