SSL: CERTIFICATE_VERIFY_FAILED 证书验证失败

SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed

from lxml import html
import requests


url = "https://website.com/"
page = requests.get(url)
tree = html.fromstring(page.content)
page.content

-> SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:748)

我运行这个脚本但是我得到了这个错误。我该怎么做?

因为你的 URL 是 "an internal corporate URL" (如评论中所述),我猜它使用自签名证书,或者是由自签名 CA 证书颁发的。

如果确实如此,您有两个选择:

(1) 将您公司 CA 的路径(包括完整的中间证书链,如果有的话)提供给 requests.get() call via verify 参数:

requests.get('https://website.lo', verify='/path/to/certfile')

(2),完全禁用客户端证书验证(但注意所有安全风险,例如简单的中间人攻击等):

requests.get('https://website.lo', verify=False)

为了完整性,相关的 verify 参数在 requests.request() 文档中有描述:

verify -- (optional) Either a boolean, in which case it controls whether we verify 
          the server's TLS certificate, or a string, in which case it must be a path 
          to a CA bundle to use. Defaults to True.