python requests请求SSL证书问题

requests发送https请求时默认验证ssl证书,此时如果访问的地址ssl证书过期失效或不信任,则请求失败,为了使请求成功,可以设置verify为False,暂时不验证

r = requests.get(url, headers=header, verify=False)

移除ssl验证后,可以访问,但会出现一长串提示:

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://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
可以看到解决办法,可用disable_warnings()移除警告

#在requests请求前添加
requests.packages.urllib3.disable_warnings()

完整代码:

import requests

requests.packages.urllib3.disable_warnings()
r = requests.get(url, headers=header, verify=False)

End

posted @ 2021-01-24 00:51  林末  阅读(2032)  评论(0编辑  收藏  举报
访客: 浏览: