使用BeautifulSoup抓取Web数据时如何修复SSL错误

时间:2019-01-04 15:56:26

标签: python ssl https beautifulsoup

我正在尝试抓取一个网站,以使用精美的汤获得一些价格。但是,由于运行代码时出现SSL证书验证错误,我无法连接到网站。

网站为here

此刻,我正在虚拟环境中运行代码。用户密钥功能有助于生成要使用的动态用户代理。

我尝试过

  • 修改用户代理
  • 使用静态URL测试更改URL是否会产生影响
  • 但是两次尝试都没有影响。

    from bs4 import BeautifulSoup
    import requests
    from userkey import getkey
    
    def kinoData(isbn,key):
        #STUB Testing
        base = "https://singapore.kinokuniya.com/bw/"
        headers = {'user-agent': key}
        url = base+isbn
    
        page = requests.get(url, headers=headers)
        #Converting it to a soup for scrapping
        soup = BeautifulSoup(page.content, 'html.parser')
        price = soup.find("li",class_="price")
    
        return price;
    
    print(kinoData("9780199536009","Mozilla/5.0 (Linux; Android 6.0.1; Nexus 4 Build/MOB30Z; wv)AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.106 Mobile Safari/537.36"))
    

    我期望获得一个带有“价格”类的li元素列表。相反,我收到此错误消息。

    requests.exceptions.SSLError: HTTPSConnectionPool(host='singapore.kinokuniya.com', port=443): Max retries exceeded with url: /bw/9780199536009 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)')))
    

    0 个答案:

    没有答案