1. 第一种方案
import requests
requests.get('https://www.zhihu.com/',verify=False)
2. 第二种方案
由于python2不支持SNI,具体SNI了解转:http://blog.csdn.net/makenothing/article/details/53292335如果想python2支持SNI,pip安装3个模块:
1.pyOpenSSL
2.ndg-httpsclient
3.pyasn1
然后在使用requests请求前添加如下代码:
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
使用第一种方案解决的时候,出现以下警告:
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.
在语句前加上以下代码即可不会被报错:requests.packages.urllib3.disable_warnings()
经过试验测试,第一,二种方案都会继续报错。
继续寻找方案(链接四):
使用requests下载日志出现:HTTPSConnectionPool(host='***', port=443): Max retries exceeded with url: ******(Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:579)'),))
分析是ssl证书报错,解决办法:
requests..get("http://...", headers={'Connection':'close'})
requests.adapters.DEFAULT_RETRIES = 5
s = requests.session() s.keep_alive = False
requests.get('https://kennethreitz.org', verify=False)
使用(1,2,3)方法即可解决:
参考:
https://www.zhihu.com/question/40025043
https://blog.csdn.net/k53247l2/article/details/74168894
https://blog.csdn.net/qq_31077649/article/details/79013199
http://www.cnblogs.com/mikeluwen/p/7244161.html