python3问题-urllib2.urlopen出现错误certificate verify failed urllib2.URLErro urlopen error [SSLCERT

urllib2.urlopen出现错误certificate ve rify failed

urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate ve rify failed (_ssl.c

我确定就在这台机器上,而且就访问这个网站的地址那就用ie访问下就可以了。
或者使用下面方法

import urllib.request as urlrequest 
#import ssl
#ssl._create_default_https_context = ssl._create_unverified_context
url_visit='https://api.douban.com/v2/movie/subject/1764796'
crawl_content=urlrequest.urlopen(url_visit).read()
print(crawl_content.decode('unicode-escape'))

而当目标网站使用的是自签名的证书时就会抛出一个 urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)> 的错误消息,

解决方案包括下列两种方式:

1. 使用ssl创建未经验证的上下文,在urlopen中传入上下文参数

import ssl
import urllib2
context = ssl._create_unverified_context()
print urllib2.urlopen("https://www.12306.cn/mormhweb/", context=context).read()
2. 全局取消证书验证

import ssl
import urllib2
 
ssl._create_default_https_context = ssl._create_unverified_context
 
print urllib2.urlopen("https://www.12306.cn/mormhweb/").read()
注意:在全全局请求文件导入import ssl

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
至此,问题圆满解决!

最近写python遇到的一些问题,望给大家一点帮助。我是小王,CSDN博客:https://blog.csdn.net/cmqwan

发布了214 篇原创文章 · 获赞 166 · 访问量 227万+
展开阅读全文

python 3 报错 urllib.error.URLError: <urlopen error unknown url type: "http>

02-19

尝试爬取新浪首页新闻到本地 程序报错 源码为: import urllib.request,re url="https://www.sina.com.cn/" req=urllib.request.Request(url) req.add_header("User-Agent","马赛克") pat1='<a target="_blank" href=(.*?)>.*?</a>' data1=urllib.request.urlopen(req).read().decode("UTF-8","ignore") allink=re.compile(pat1).findall(data1) for i in range(0,len(allink)): thislink=allink[i] pat2='<frame src=(.*?)>' req2=urllib.request.Request(url) req2.add_header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:65.0) Gecko/20100101 Firefox/65.0") thispage=urllib.request.urlopen(req2).read().decode("UTF-8","ignore") isframe=re.compile(pat2).findall(thispage) if len(isframe)==0: urllib.request.urlretrieve(thislink,"data/"+str(i)+".html") else: flink=isframe[0] urllib.request.urlretrieve(flink,"data/"+str(i)+".html") 报错信息: Traceback (most recent call last): File "/Users/tanzhouyan/Desktop/python/新闻爬虫.py", line 73, in <module> urllib.request.urlretrieve(thislink,"data/"+str(i)+".html") File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 247, in urlretrieve with contextlib.closing(urlopen(url, data)) as fp: File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 548, in _open 'unknown_open', req) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1387, in unknown_open raise URLError('unknown url type: %s' % type) urllib.error.URLError: <urlopen error unknown url type: "http> 在网上一直没有找到解决方法,谢谢大家~ 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览