【问题标题】:ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:749)
【发布时间】:2017-06-01 04:09:36
【问题描述】:

我正在尝试使用来自 Github (https://github.com/gto76/betbrain-scraper) 的 betbrain.py,它具有以下代码:

#!/usr/bin/python3
#
# Usage: betbrain.py [URL or FILE] [OUTPUT-FILE]
# Scrapes odds from passed betbrain page and writes them to
# stdout, or file if specified.

import os
import sys
import urllib.request


from bs4 import BeautifulSoup
from http.cookiejar import CookieJar

import parser_betbrain
import printer

DEFAULT_URL = 'https://www.betbrain.com/football/england/premier-league/#!/matches/'


# If no arguments are present, it parses the default page.
# Argument can be an URL or a local file.
def main():
  html = getHtml(sys.argv)
  soup = BeautifulSoup(html, "html.parser")
  matches = parser_betbrain.getMatches(soup)
  string = printer.matchesToString(matches)
  output(string, sys.argv)

def getHtml(argv):
  if len(argv) <= 1:
    return scrape(DEFAULT_URL)
  elif argv[1].startswith("http"):
    return scrape(argv[1])
  else:
    return readFile(argv[1])

# Returns html file located at URL.
def scrape(url):
  cj = CookieJar()
  opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
  try:
    return opener.open(url)
  except ValueError:
    error("Invalid URL: " + url)

def readFile(path):
  try:
    return open(path, encoding='utf8')
  except IOError:
    error("Invalid input filename: " + path)

def output(string, argv):
  if len(argv) <= 2:
    print(string)
  else:
    writeFile(argv[2], string)

def writeFile(path, string):
  try:  
    fo = open(path, "w", encoding='utf8')
    fo.write(string);
    fo.close()
  except IOError:
    error("Invalid output filename: " + path)

def error(msg):
  msg = os.path.basename(__file__)+": "+msg
  print(msg, file=sys.stderr)
  sys.exit(1)

if __name__ == '__main__':
  main()

但是运行时又出现这个错误

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 401, in wrap_socket
    _context=self, _session=session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 808, in __init__
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1061, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Daniel/Desktop/betbrain-scraper-master 2/betbrain.py", line 71, in <module>
    main()
  File "/Users/Daniel/Desktop/betbrain-scraper-master 2/betbrain.py", line 22, in main
    html = getHtml(sys.argv)
  File "/Users/Daniel/Desktop/betbrain-scraper-master 2/betbrain.py", line 30, in getHtml
    return scrape(DEFAULT_URL)
  File "/Users/Daniel/Desktop/betbrain-scraper-master 2/betbrain.py", line 41, in scrape
    return opener.open(url)
  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:749)>

我该如何解决这个问题?我在 MacOS 10.12.1 上运行 Python 3.6.0

【问题讨论】:

    标签: python python-3.x ssl


    【解决方案1】:

    打开终端看看:

    /Applications/Python 3.6/Install Certificates.command
    

    MacOS 上的 Python 3.6 使用嵌入式版本的 OpenSSL,它不使用系统证书存储。更多详情here.

    (明确地说:MacOS 用户可能可以通过打开 Finder 并双击 Install Certificates.command 来解决)

    【讨论】:

    • 这个命令修复了stackoverflow.com/questions/38835270/…中描述的另一个问题
    • 我对使用 pyenv 安装的 py36 确实有同样的问题,所以这个命令不存在。此外,我能够使用 python 2.7 复制相同的错误
    • 如果没有root权限,有没有解决办法?我做了pip3 install --user certifi,但并没有解决问题。
    • 这种方法帮助我解决了另一个令人头疼的问题:github.com/fangli/django-saml2-auth/issues/24。奇怪的是,默认安装程序没有安装此命令,因为它似乎以类似不明显的方式破坏了其他一些 python3.6 程序(对于 rss 阅读器)。
    • 为我工作。但不是通过双击给Permision Denied 我所做的是cd Applications/Python\ 3.6 然后sudo ./Install\ Certificates.command 这对我有用
    【解决方案2】:

    我在 MacOS 上运行了这个 /Applications/Python\ 3.6/Install\ Certificates.command

    【讨论】:

    • 你太棒了兄弟。
    【解决方案3】:

    这是我修复它的方法:

    1. 打开了 Install Cerificates.Command。shell 脚本已被执行。
    2. 打开 Python 3.6.5 并输入nltk.download()。下载图形窗口打开并安装了所有包。

    【讨论】:

      【解决方案4】:

      在 CentOS Linux,Python3.6 上,我编辑了这个文件(先做一个备份)

      /usr/lib/python3.6/site-packages/certifi/cacert.pem
      

      在文件末尾,我从我的 .pem 文件中添加了我的公共证书。 您应该能够从您的 ssl 证书提供商处获取 .pem 文件。

      【讨论】:

        【解决方案5】:

        我在官方 Python docker 中遇到了同样的问题,并通过在运行任何 nltk 代码之前运行 apt-get update &amp;&amp; apt-get install ca-certificates 解决了它。

        【讨论】:

          【解决方案6】:

          在 windows 中,在顶部添加这两行可以快速解决:

          import ssl
          ssl._create_default_https_context = ssl._create_unverified_context
          

          【讨论】:

            猜你喜欢
            • 2019-03-12
            • 2017-06-25
            • 2018-07-29
            • 2023-03-03
            • 1970-01-01
            • 2018-03-15
            • 2020-01-14
            • 1970-01-01
            • 2017-02-05
            相关资源
            最近更新 更多