有些网站https需要证书才可以访问, 两种解决方法:
一、指定证书

指定一个本地证书用作客户端证书,可以是单个文件(包含密钥和证书)或一个包含两个文件路径的元组

import ssl
 
requests.get('https://kennethreitz.org', cert=('/path/client.cert', '/path/client.key'))

二、忽略证书(不安全)

如果你将 verify 设置为 False,Requests 也能忽略对 SSL 证书的验证

import requests
from requests.packages import urllib3
 
urllib3.disable_warnings()
req = requests.get('https://grwsyw.bjgjj.gov.cn/ish/',verify=False)
print(req.text)

解决warning的方式:忽略验证警告信息

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

InsecureRequestWarning)


翻译:

警告:正在发出未经验证的HTTPS请求。强烈建议添加证书验证。见:https://urllib3.readthedocs.io/en/latest/advanced-usage.html ssl-warnings


InsecureRequestWarning)

解决方式:

新版本解决方式:

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

旧版本解决方式:

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐