python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)
1.参考
Py 坑之 CERTIFICATE_VERIFY_FAILED
Python 升级到 2.7.9 之后引入了一个新特性,当你urllib.urlopen一个 https 的时候,会验证一次 SSL 证书,当目标网站使用的是自签名的证书时就会爆出一个 urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)> 的错误消息
Python Requests throwing up SSLError
The problem you are having is caused by an untrusted SSL certificate.
Like @dirk mentioned in a previous comment, the quickest fix is setting verify=False.
Please note that this will cause the certificate not to be verified. This will expose your application to security risks, such as man-in-the-middle attacks.
Of course, apply judgment. As mentioned in the comments, this may be acceptable for quick/throwaway applications/scripts, but really should not go to production software.
If just skipping the certificate check is not acceptable in your particular context, consider the following options, your best option is to set the verify parameter to a string that is the path of the .pem file of the certificate (which you should obtain by some sort of secure means).
So, as of version 2.0, the verify parameter accepts the following values, with their respective semantics:
True: causes the certificate to validated against the library's own trusted certificate authorities (Note: you can see which Root Certificates Requests uses via the Certifi library, a trust database of RCs extracted from Requests: Certifi - Trust Database for Humans).False: bypasses certificate validation completely.- Path to a CA_BUNDLE file for Requests to use to validate the certificates.
Source: Requests - SSL Cert Verification
Also take a look at the cert parameter on the same link.
2.解决办法
(1)全局设置,对requests不生效
ssl._create_default_https_context = ssl._create_unverified_context
(2)urllib2.urlopen 传参
context = ssl._create_unverified_context() urllib2.urlopen(context=context)
(3)参考 urllib2.urlopen 为opener叠加handler
# C:\Program Files\Anaconda2\Lib\urllib2.py
# def urlopen
# elif context:
# https_handler = HTTPSHandler(context=context)
# opener = build_opener(https_handler) # urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
try:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
resp = opener.open(req)
except urllib2.URLError as err:
print err
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar), urllib2.HTTPSHandler(context=context)) #叠加多个 handler
resp = opener.open(req)
(4)request.get 传参
# requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
try:
r = requests.get(url, headers=headers, cookies=cookie)
except requests.exceptions.SSLError as err:
print err
r = requests.get(url, headers=headers, cookies=cookie, verify=False)
python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)的更多相关文章
- [python][nginx][https] Nginx 服务器 SSL 证书安装部署
目录 前言 1 申请证书 2 Nginx 服务器 SSL 证书安装部署 2.1.准备 Nginx 环境 2.2 证书部署 2.3 Nginx 配置 3 最后 参考链接 前言 博主博客中的图片,使用的是 ...
- 解决 https urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] 错误
import ssl ssl._create_default_https_context = ssl._create_unverified_context
- python https请求报错:SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED]
python爬虫,使用requests库发送https请求报错:SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 解决方法: imp ...
- Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED
Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描述 使用pip按照virtualenv报错,如下: pip install virtua ...
- python接口自动化(十二)--https请求(SSL)(详解)
简介 本来最新的requests库V2.13.0是支持https请求的,但是一般写脚本时候,我们会用抓包工具fiddler,这时候会 报:requests.exceptions.SSLError: [ ...
- Python [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 解决方法
一个搭建在SAE上的Django应用,使用新浪微博提供的Python SDK已经稳定运行一年有余,但最近开始持续出现微博认证失败的状况. 摘录微博python SDK的错误提示如下所示: ERROR: ...
- Python djangorestframework安装库报错SSL: CERTIFICATE_VERIFY_FAILED
Python djangorestframework 安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描述 使用pip按照virtualenv报错,如下: pip inst ...
- Python做简单爬虫(urllib.request怎么抓取https以及伪装浏览器访问的方法)
一:抓取简单的页面: 用Python来做爬虫抓取网站这个功能很强大,今天试着抓取了一下百度的首页,很成功,来看一下步骤吧 首先需要准备工具: 1.python:自己比较喜欢用新的东西,所以用的是Pyt ...
- 【Python】【BugList13】req = requests.get(url=target)报错: (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)')
[代码] # -*- coding:UTF-8 -*- import requests if __name__ == '__main__': target = 'https://unsplash.co ...
随机推荐
- Mudo C++网络库第二章学习笔记
线程同步的精要 并发有两种基本的模型: 一种是message passing(消息传递); 另一种是shared memory(共享内存); 在分布式系统中(有多台物理机需要通信), 运行在多台机器上 ...
- OpenStack实践系列②认证服务Keystone
OpenStack实践系列②认证服务Keystone 三.实战OpenStack之控制节点3.1 CentOS7的时间同步服务器chrony 下载chrony # yum install -y chr ...
- C#如何使用SqlCacheDependency
1.数据库依赖类SqlCacheDependency 数据库缓存依赖主要解决的是当数据库的内容发生改变时,如何及时通知缓存,并更新缓存中的数据的问题. 语法定义: SqlCacheDependency ...
- ZOJ 2110 DFS
狗要出门,且正好在T秒 就是DFS + 剪枝, 联系一下剪枝技巧 #include<iostream> #include<cstdio> #include<cstring ...
- [HTTP]Etag的工作流程
1. 浏览器首次访问该资源时,web服务器返回资源的同时,响应报文头携带ETag标签: 2. 浏览器将保存该Etag标签的值: 3. 当浏览器发起下一次请求,请求报文头将会携带 If-None-Mat ...
- swift 学习- 10 -- 类和结构体
// '类和结构体' 是人们构建代码所使用的一种通用且灵活的构造体, 我们可以使用完全相同的语法规则来为 '类和结构体' 定义属性 (变量 和 常量) 和添加方法, 从而扩展 类和结构体 的功能 // ...
- 《Oracle DBA工作笔记:运维、数据迁移与性能调优》 PDF 下载
一:下载途径 二:本书图样 三:本书目录 第1篇 数据库运维篇第1章 数据库安装配置1.1 安装前的准备 11.2 安装数据库软件 51.2.1 方法1:OUI安装 61.2.2 方法2:静默安装 8 ...
- flutter No material widget found textfield widgets require a material widget ancestor
Error states that TextField widgets require a Material widget ancestor. Simply wrapping your whole l ...
- 网络扫描信息收集基于(Windows)
1.首先说明一下一款网络扫描工具,在之前的博客中我曾简要的写过关于Advance IP Scanner使用方法,最近要写网络扫描的工具,所以对这款工具做一个详细的功能细节上的介绍. 如下图 在输入框 ...
- application program Can't Start
一.电脑插U盘中毒之后感染,出现无法启动,但是风扇工作,先拔掉电源,卸掉电池之后重新安装,重启电脑,之后发现电脑桌面上的程序无法启动,360webShield也无法启动,可能是电脑中毒后的假死机现象. ...