【python3】urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>

在玩爬虫的时候,针对https ,需要单独处理。不然就会报错:

 

 解决办法:引入 ssl 模块即可 

核心代码

imort ssl
ssl._create_default_https_context = ssl._create_unverified_context

完整代码如下:

# coding=utf-8
import re
import urllib.request
import ssl


# 获取html内容
def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    html = html.decode('utf-8')
    return html


# 获取title
def get_title(html):
    reg = r'<title>(.*)</title>'
    content_title = re.compile(reg)
    result = re.findall(content_title, html)
    return result


# 创建ssl证书
ssl._create_default_https_context = ssl._create_unverified_context
url = "https://www.cnblogs.com"
html = getHtml(url)
title = get_title(html)

print(title)

 结果: 

 

posted @ 2018-04-14 16:00  依然范儿特西  阅读(3992)  评论(1)    收藏  举报