python 爬虫:https; HTTPSConnectionPool(host='z.jd.com', port=443)

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 requestisbeing made. Adding certificate verificationisstrongly 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证书报错,解决办法:

1. requests默认是keep-alive的,可能没有释放,加参数 headers={'Connection':'close'}

requests..get("http://...", headers={'Connection':'close'})

2. 增加连接重试次数: 

requests.adapters.DEFAULT_RETRIES =5

3. 关闭多余的连接:requests使用了urllib3库,默认的http connection是keep-alive的,requests设置False关闭。

s = requests.session() 

 s.keep_alive = False

操作方法:

s = requests.session()      

 s.keep_alive =False

4. 不用ssl证书验证: 

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容