【问题标题】:Using urllib gives SSL error使用 urllib 会产生 SSL 错误
【发布时间】:2018-06-12 14:52:25
【问题描述】:

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


    【解决方案1】:

    不久前我遇到了这个问题。 尝试使用以下命令:

    1. pip 安装请求[安全]

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

    1. pip install pyOpenSSL
    2. pip 安装密码
    3. pip install idna

    【讨论】:

    • 我会试一试,让你知道结果
    【解决方案2】:

    试试 requests 模块。我正在使用 Windows,因此请尝试使用 pip 安装的 mac 等效项。对于 Python 3.6,我相信 urllib 一直存在一些问题。如果您尝试进行网络抓取,则 requests 模块比 urllib 模块更适合 Python 3。

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

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

    我通过 requests 模块获得了 url,然后能够使用 文本使用美丽的汤。根据我收集的信息,对于 Python 3+,应始终避免使用 urllib,并且更适合 Python 3。尝试完全按照您正在做的事情,但通过请求模块而不是 urllib。

    【讨论】:

    • 出于这个原因,我实际上更喜欢使用请求。但我需要为 Amazon 使用 API,而我知道的唯一方法是为使用 urllib 的 API 使用预先存在的代码。如果您有任何机会知道使用 Amazon API 的任何其他方式,那将会很有帮助。
    猜你喜欢
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多