调用urlopen()时ssl出错解决方法

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)>
复制代码

经过修改模块设置为不用验证证书便可html

from urllib.request import urlopen
from bs4 import BeautifulSoup
import logging
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
try:
    url = "/wiki/Morgan_Freeman"
    res_html = urlopen("https://en.wikipedia.org"+url).read()  # return HTML of the website
    soup = BeautifulSoup(res_html, 'html.parser')  # beautiful soup object to be used for parsing html content
    print(soup)
except:
    logging.warning("Bad website url: %s", url)
复制代码
相关文章
相关标签/搜索