使用 urllib 会导致 SSL 错误 [英] Using urllib gives SSL error
问题描述
使用 urllib 运行请求但不断收到此错误:
Running a request with urllib but continuously receive this error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect
server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 814, in __init__
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1068, in do_handshake
self._sslobj.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
products = amazon.search_n(1, Keywords='kindle', SearchIndex='All')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/amazon/api.py", line 288, in search_n
return list(islice(items, n))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/amazon/api.py", line 544, in __iter__
for page in self.iterate_pages():
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/amazon/api.py", line 561, in iterate_pages
yield self._query(ItemPage=self.current_page, **self.kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/amazon/api.py", line 573, in _query
response = self.api.ItemSearch(ResponseGroup=ResponseGroup, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bottlenose/api.py", line 274, in __call__
{'api_url': api_url, 'cache_url': cache_url})
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bottlenose/api.py", line 235, in _call_api
return urllib2.urlopen(api_request, timeout=self.Timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/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:777)>
有什么想法可以解决这个问题吗?显然 SSL 验证工作不正常,但我不知道如何解决这个问题.
Any ideas how this can be resolved? Evidently the SSL verification is not working correctly, but I do not know how to fix the issue.
在 Mac 操作系统上使用 Python 3.6.1
Using Python 3.6.1 on Mac Os
谢谢
推荐答案
尝试请求模块.我正在使用 Windows,因此请尝试使用 pip 安装等效的 mac.对于 Python 3.6,我相信 urllib 一直存在一些问题.如果您正在尝试抓取网页,则 requests 模块比 urllib 模块更适合 Python 3.
Try the requests module. I am using a windows, so try the mac equivalent of a pip install. With Python 3.6, I believe that urllib has been having some issues. If you are trying to webscrape, the requests module is better for Python 3, than the urllib module.
下面是我前几天在维基百科上搜索股票行情时的一个例子:
Below is an example from the other day when I was webscraping Wikipedia for stock tickers:
response = requests.get("https://en.wikipedia.org/wiki/List_of_S26P_500_companies")
soup = bs4.BeautifulSoup(response.text)
我通过使用请求模块获得了 url,然后能够使用文本使用美丽的汤.从我收集到的信息来看,对于 Python 3+ 来说,urllib 在任何时候都应该避免,并且更适合 Python 3.尝试完全按照你正在做的事情做,但是通过 requests 模块而不是 urllib.
I got the url by using the requests module, and then was able to work with the text using beautiful soup. From what I gathered, urllib is to be avoided at all times for Python 3+ and is more suitable for Python 3. Try to do exactly what you're doing, but via the requests module instead of urllib.
这篇关于使用 urllib 会导致 SSL 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!