[英]SSL: CERTIFICATE_VERIFY_FAILED certificate verify failed in Python
[英]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 是“内部公司 URL”(如评论中所述),我猜它使用自签名证书,或者由自签名 CA 证书颁发。
如果确实如此,您有两种选择:
(1) 通过verify
参数提供到requests.get()
调用的公司 CA(包括完整的中间证书链,如果有)的路径:
requests.get('https://website.lo', verify='/path/to/certfile')
或(2) ,完全禁用客户端证书验证(但要注意这带来的所有安全风险,例如简单的中间人攻击等):
requests.get('https://website.lo', verify=False)
为了完整起见,requests.request()
文档中描述了相关的verify
参数:
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.
在不使用SSL的情况下使用以下代码
from lxml import html
import requests
url = "http://website.com/"
page = requests.get(url)
tree = html.fromstring(page.content)
page.content
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.