BeautifulSoup-urlopen错误[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败

时间:2018-08-30 07:18:53

标签: python-3.x beautifulsoup

我正在尝试解析Socialblade页面以获取一些YouTube信息,但始终出现以下错误:

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

我该如何解决?到目前为止,这是我的代码:

def process_socialblade(username):
    socialblade_user_url = 'https://socialblade.com/youtube/user/' + username
    context = ssl._create_unverified_context()
    print('Attempting to parse Socialblade:', socialblade_user_url)
    q = Request(socialblade_user_url)
    q.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) '
                               'Chrome/23.0.1271.64 Safari/537.11')
    q.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
    q.add_header('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')
    q.add_header('Accept-Encoding', 'none')
    q.add_header('Accept-Language', 'en-US,en;q=0.8')
    q.add_header('Connection', 'keep-alive')
    socialblade_user_html = urlopen(q, context).read()
    if socialblade_user_html:
        soup = BeautifulSoup(socialblade_user_html, "html.parser")
        youtube_user_top_info_list = soup.findAll('div', attrs={'class': 'YouTubeUserTopInfo'})
        for i in youtube_user_top_info_list:
            print(i.text())
    else:
        print('Could not parse Socialblade', socialblade_user_html)

0 个答案:

没有答案