以下のコードを実行するとエラーが出ます。
原因がわかりません。
該当のソースコード
import urllib.request from bs4 import BeautifulSoup class Scraper: def __init__(self, site): self.site = site def scrape(self): response = urllib.request.urlopen(self.site) html = response.read() soup = BeautifulSoup(html, 'html.parser') with open("output.txt", "w") as f: for tag in soup.find_all('a'): url = tag.get('href') if url and 'html' in url: print("\n" + url) f.write(url + "\n") Scraper('https://news.google.com/').scrape()
上記のコードを実行すると以下のエラーが発生します。(長いです)
Python3.7.Traceback
1 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open 2 encode_chunked=req.has_header('Transfer-encoding')) 3 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request 4 self._send_request(method, url, body, headers, encode_chunked) 5 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request 6 self.endheaders(body, encode_chunked=encode_chunked) 7 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders 8 self._send_output(message_body, encode_chunked=encode_chunked) 9 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output 10 self.send(msg) 11 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send 12 self.connect() 13 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1392, in connect 14 server_hostname=server_hostname) 15 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket 16 session=session 17 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 850, in _create 18 self.do_handshake() 19 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1108, in do_handshake 20 self._sslobj.do_handshake() 21ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045) 22 23During handling of the above exception, another exception occurred: 24 25Traceback (most recent call last): 26 File "/Users/okuharasho/Documents/scraper.py", line 20, in <module> 27 Scraper('https://news.google.com/').scrape() 28 File "/Users/okuharasho/Documents/scraper.py", line 10, in scrape 29 response = urllib.request.urlopen(self.site) 30 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen 31 return opener.open(url, data, timeout) 32 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open 33 response = self._open(req, data) 34 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open 35 '_open', req) 36 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain 37 result = func(*args) 38 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1360, in https_open 39 context=self._context, check_hostname=self._check_hostname) 40 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open 41 raise URLError(err) 42urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)> 43
試したこと
最後の方のurllib.error.URLError: 以降のエラーコードをググってみたところSSL認証?が降りない ような事が言われていて、以下のコードを加えて実行したところ、確かにエラーコードは出ないのですがシェルになんの反応もありませんでした。
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
回答1件
あなたの回答
tips
プレビュー