使用urllib会出现SSL错误

问题描述 投票:0回答:2

使用urllib运行请求但不断收到此错误:

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验证工作不正常,但我不知道如何解决问题。

在Mac Os上使用Python 3.6.1

谢谢

python ssl urllib2 urllib urllib3
2个回答
0
投票

不久前我遇到过这个问题。尝试给出以下命令:

  1. pip安装请求[安全]

如果这不起作用,请尝试以下操作:

  1. pip install pyOpenSSL
  2. pip安装加密
  3. 这是Pip Install

0
投票

尝试请求模块。我正在使用Windows,所以尝试相当于一个pip安装的mac。使用Python 3.6,我相信urllib一直存在一些问题。如果您正在尝试使用webscrape,则请求模块比urllib模块更适合Python 3。

下面是我在维基百科上搜索股票代码的另一天的例子:

          response = requests.get("https://en.wikipedia.org/wiki/List_of_S26P_500_companies")
          soup = bs4.BeautifulSoup(response.text)

我通过使用请求模块得到了网址,然后能够使用美丽的汤来处理文本。从我收集的内容来看,对于Python 3+来说,urllib应该始终被避免,并且更适合Python 3.尽量完成您正在做的事情,但是通过请求模块而不是urllib。

© www.soinside.com 2019 - 2024. All rights reserved.