urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)>
发生了这样的错误,但是在Windows10下就不会发生,查到了CSDN一许流星的博客,发现可以这样解决,但是只是解决问题,并没有理解问题发生的原因
在他博客中
可能原因分析:
Python 2.7.9 之后引入了一个新特性
当你urllib.urlopen一个 https 的时候会验证一次 SSL 证书
当目标使用的是自签名的证书时就会爆出一个
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate
verify failed (_ssl.c:581)> 的错误消息
处理方式如下:
import ssl
# This restores the same behavior as before.
context = ssl._create_unverified_context()
response = urllib.request.urlopen("https://no-valid-cert", context=context)
如此这样那我在网上copy的代码(爬去熊猫TV英雄联盟的主播rank)就可以完美运行
import re
from urllib import request
import ssl
context = ssl._create_unverified_context()
class Spider():
# 匹配所有字符 [\s\S]*? 非贪婪
url = 'https://www.panda.tv/cate/lol'
root_pattern = '<div class="video-info">([\w\W]*?)</div>'
name_pattern = '</i>([\w\W]*?)</span>'
number_pattern = '<span class="video-number">([\w\W]*?)</span>'
def __fetch_content(self):
r = request.urlopen(Spider.url, context=context)
# 字节码
htmls = r.read()
htmls = str(htmls, encoding='utf-8')
return htmls
def __analysis(self, htmls):
root_html = re.findall(Spider.root_pattern, htmls)
anchors = []
for html in root_html:
name = re.findall(Spider.name_pattern, html)
number = re.findall(Spider.number_pattern, html)
anchor = {'name': name, 'number': number}
anchors.append(anchor)
return anchors
def __refine(self, anchors):
# 匿名函数lambda
l = lambda anchor: {'name': anchor['name'][0].strip(), 'number': anchor['number'][0]}
return map(l, anchors)
def __sort(self, anchors):
# 默认增序
anchors = sorted(anchors, key=self.__sort_seed, reverse=True)
return anchors
def __sort_seed(self, anchor):
r = re.findall('\d*', anchor['number'])
number = float(r[0])
if '万' in anchor['number']:
number *= 10000
return number
def __show(self, anchors):
for rank in range(0, len(anchors)):
print('rank' + str(rank + 1) + ':' + anchors[rank]['name'] + ' ' + anchors[rank]['number'])
def go(self):
htmls = self.__fetch_content()
anchors = self.__analysis(htmls)
anchors = list(self.__refine(anchors))
anchors = self.__sort(anchors)
self.__show(anchors)
spider = Spider()
spider.go()
最后
网友评论