使用urllib会出现SSL错误

时间:2018-01-02 22:34:47

标签: python ssl urllib2 urllib urllib3

使用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

由于

2 个答案:

答案 0 :(得分:0)

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

  1. pip安装请求[安全]
  2. 如果这不起作用,请尝试以下操作:

    1. pip install pyOpenSSL
    2. pip install cryptography
    3. pip install idna

答案 1 :(得分:0)

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

以下是我在维基百科上搜索股票行情的另一天的例子:

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

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