python使用requests时报错requests.exceptions.SSLError: HTTPSConnectionPool
报错信息
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 640, in send
history = [resp for resp in gen] if allow_redirects else []
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 218, in resolve_redirects
**adapter_kwargs
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\adapters.py", line 506, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),))
过程
测试1
不指定headers时GET:
>>> import requests
>>> requests.get('http://www.baidu.com/')
<Response [200]>
>>> requests.get('http://www.baidu.com/')
<Response [200]>
>>> requests.get('http://www.baidu.com/')
<Response [200]>
>>> header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',}
>>> requests.get('http://www.baidu.com/', headers = header)
<Response [200]>
测试2
当指定headers的User-Agent为火狐浏览器时:
>>> header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',}
>>> requests.get('http://www.baidu.com/', headers = header)
<Response [200]>
>>> requests.get('http://www.baidu.com/', headers = header)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 640, in send
history = [resp for resp in gen] if allow_redirects else []
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 218, in resolve_redirects
**adapter_kwargs
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "D:\python\lib\site-packages\requests-2.18.3-py2.7.egg\requests\adapters.py", line 506, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),))
分析
现象:第一次GET时正常,第二次GET时,会报错.
不同点:User-Agent不相同
分析:由于报错SSL证书验证失败,所以这次的访问应该是https协议.但是我们明明使用的是http,所以,猜测访问该网站后,被重定向到了https://www.baidu.com/
验证
首先,进行GET时,关闭证书验证.因为,如果不关闭,请求总是失败,不能获取到重定向的信息.
>>> response = requests.get('http://www.baidu.com/', headers = header, verify=False)
D:\python\lib\site-packages\urllib3\connectionpool.py:858: 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)
>>> response.history
[<Response [302]>]
>>> response.url
u'https://www.baidu.com/'
当不指定User-Agent时
>>> response = requests.get('http://www.baidu.com/', verify=False)
>>> response.history
[]
>>> response.url
u'http://www.baidu.com/'
结论
当指定headers的User-Agent时,baidu的服务器会重定向到https的网址.因此报出SSL验证失败的错误.
解决方法
方法1:
在进行GET时,指定SSL证书.详情见附件
方法2:
关闭证书验证. 详情见附件
附件
[各浏览器的User-Agent] http://www.useragentstring.com/pages/useragentstring.php
[SSL 证书验证] http://docs.python-requests.org/zh_CN/latest/user/advanced.html#ssl
转自:https://blog.csdn.net/win_turn/article/details/77142100
python使用requests时报错requests.exceptions.SSLError: HTTPSConnectionPool的更多相关文章
- 解决python爬虫requests.exceptions.SSLError: HTTPSConnectionPool(host='XXX', port=443)问题
爬虫时报错如下: requests.exceptions.SSLError: HTTPSConnectionPool(host='某某某网站', port=443): Max retries exce ...
- 关于requests.exceptions.SSLError: HTTPSConnectionPool
问题: requests.exceptions.SSLError: HTTPSConnectionPool(host='mall.christine.com.cn', port=443): Max r ...
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib
python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...
- python 读取文件时报错UnicodeDecodeError
python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...
- python2.7使用requests时报错SSLError: HTTPSConnectionPool(host='b-ssl.duitang.com', port=443)
import requests url='https://www.duitang.com/napi/blog/list/by_search/?kw=%E6%A0%A1%E8%8A%B1&sta ...
- python 创建虚拟环境时报错OSError, setuptools下载失败
错误信息如下: Using base prefix 'c:\\users\\huful\\appdata\\local\\programs\\python\\python36-32'New pytho ...
- 在用python操作mysql时报错:ModuleNotFoundError: No module named 'MySQLdb'
用Flask+python+mysql写一个小项目 系统 win10 py版本:3.6.1 在配置数据库时报错ModuleNotFoundError: No module named 'MySQLdb ...
- Pycharm 在 import requests 时报错
requests的安装我尝试了两种方法: 第一种:下载安装 https://github.com/requests/requests 然后解压到:(python的安装路径,这里使用的是默认路径) Ap ...
- selenium +python webdriver运行时报错cannot find Chrome binary
今日在公司电脑运行自动化测试脚本,出现cannot find Chrome binary报错 百思不得其解,排错后发现应该是电脑以前有配置driver文件路径,driver所在文件路径已变更,现pyt ...
随机推荐
- 【laravel5.*】添加ide_helper.php 助手
1.参照文档:https://github.com/barryvdh/laravel-ide-helper#automatic-phpdoc-generation-for-laravel-facade ...
- Textview文字监控(输入到某个字符后,进行操作)
以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换. 1.首先给用户添加一个textchangedlistener 2.然后再写一个文字变化的监视器 mobile_et.add ...
- kubernetes-pod分配CPU和RAM资源以及计算方法
例子: Containers: cpu-ram-demo-container: ... image: busybox:latest resources: Limits: cpu: 1 memory: ...
- DPDK的安装与绑定网卡(转)
from:http://www.cnblogs.com/mylinuxer/p/4274178.html DPDK的安装与绑定网卡 DPDK的安装有两种方法: 第一种是使用dpdk/tools/set ...
- Nginx+FastCGI运行原理(二)
1.4 PHP与PHP-FPM的安装及优化(2) 标签rlimit_files用于设置PHP-FPM对打开文件描述符的限制,默认值为1024.这个标签的值必须和Linux内核打开文件数关联起来,例如, ...
- sqlserver学习笔记(三)—— 为数据库添加新的用户
首先,用windows或sa身份登录sqlserver, 打开安全性——登录名——右键新建登录名:在选择页——常规中,新建命为user_b的登录名,选择sqlserver身份验证方式,设置密码确认密码 ...
- 【Android】详解Android的menu菜单
在软件应用过程中,菜单的存在是必不可少的,我这次讲一下,我对android菜单的一个基础做法吧 Android的菜单分为三种类型:选项菜单(Option Menu).上下文菜单(Context Men ...
- 关于解锁美版Play市场
关于解锁美版Play市场(本帖已经突破了G+的500评论上限,如有问题请转载留言) 我对这个问题思考的很深刻也思考了很久,作为一个深度google脑残粉怎么能用不完整的Play Store呢?那人生岂 ...
- ELK 5.X版本遇到的坑
一.Kafka ->logstash ->elasticsearch logstash 5.X以上版本不兼容5.x以下版本,因此在升级logstash时会遇到很多坑.首先是配置的变化 ...
- wps 根据单元格值 设置单元格所在行 颜色(大于0 行红色 小于0 行xx色)