报错信息

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的更多相关文章

  1. 解决python爬虫requests.exceptions.SSLError: HTTPSConnectionPool(host='XXX', port=443)问题

    爬虫时报错如下: requests.exceptions.SSLError: HTTPSConnectionPool(host='某某某网站', port=443): Max retries exce ...

  2. 关于requests.exceptions.SSLError: HTTPSConnectionPool

    问题: requests.exceptions.SSLError: HTTPSConnectionPool(host='mall.christine.com.cn', port=443): Max r ...

  3. 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 ...

  4. python 读取文件时报错UnicodeDecodeError

    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...

  5. 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 ...

  6. python 创建虚拟环境时报错OSError, setuptools下载失败

    错误信息如下: Using base prefix 'c:\\users\\huful\\appdata\\local\\programs\\python\\python36-32'New pytho ...

  7. 在用python操作mysql时报错:ModuleNotFoundError: No module named 'MySQLdb'

    用Flask+python+mysql写一个小项目 系统 win10 py版本:3.6.1 在配置数据库时报错ModuleNotFoundError: No module named 'MySQLdb ...

  8. Pycharm 在 import requests 时报错

    requests的安装我尝试了两种方法: 第一种:下载安装 https://github.com/requests/requests 然后解压到:(python的安装路径,这里使用的是默认路径) Ap ...

  9. selenium +python webdriver运行时报错cannot find Chrome binary

    今日在公司电脑运行自动化测试脚本,出现cannot find Chrome binary报错 百思不得其解,排错后发现应该是电脑以前有配置driver文件路径,driver所在文件路径已变更,现pyt ...

随机推荐

  1. Easyui入门视频教程 第03集---Easyui布局

    Easyui入门视频教程 第03集---Easyui布局 目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标自定义   Easyui入门视频教 ...

  2. shell脚本逐个杀死k8s中某个应用的pod

    #!/bin/bash pod01=`kubectl get pod -o wide -n weifeng-system|grep official-ui-node-prod|awk -F : 'NR ...

  3. java struts2入门学习--基于xml文件的声明式验证

    一.知识点总结 后台验证有两种实现方式: 1 手工验证顺序:validateXxx(针对Action中某个业务方法验证)--> validate(针对Action中所有的业务方法验证) 2 声明 ...

  4. iOS绘图UIBezierPath 和 Core Graphics框架

    前言 iOS系统本身提供了两套绘图的框架,即UIBezierPath 和 Core Graphics.而前者所属UIKit,其实是对Core Graphics框架关于path的进一步封装,所以使用起来 ...

  5. 【LeetCode】227. Basic Calculator II

    Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...

  6. (转)失败和拒绝,也是一种肯定 找工作时,我四处碰壁这一段经历对自己职业生涯的帮助最大。为什么? "因为这些挫折让我的脸皮变厚了 如果你不是每天被人拒绝,那就说明你的人生目标不够远大 所谓成功,就是不停地经历失败,并且始终保持热情

    (转)失败和拒绝,也是一种肯定 昨天,先是看到一个老外,说了一句很震撼的话. "你个人的项目,应该有四分之一会失败,否则就说明你的冒险精神不够." (Expect and hope ...

  7. 归并排序(C++实现)

         归并排序是利用"归并"技术来进行排序.归并是指将若干个已排序的子文件合并成一个有序的文件.常见的归并排序有两路归并排序(Merge Sort),多相归并排序(Polyph ...

  8. C 简单1

    #include <stdio.h> #define Height 10 int main(){ int width; int clong; int result; printf(&quo ...

  9. SDL操作笔记

    原文地址:https://blog.csdn.net/qq_38124598/article/details/73543184 这里总结一下个人对SDL的一些函数,方便以后查询.   v1.0.1 1 ...

  10. 《JAVA与模式》之原型模式(转载)

    原型模式其实就是java的拷贝机制 原文出处:http://blog.csdn.net/zhengzhb/article/details/7393528   定义:用原型实例指定创建对象的种类,并通过 ...