简体   繁体   中英

Why can't I use requests library to access a website while I can still visit it from the browser?

I have this function to access a website.

def access_website(link):
        
    try:
        
        cert = requests.certs.where()
        page = requests.get(link, 
                            verify=cert, 
                            headers={"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"})
                
        return page

    except Exception as x:
        print(x)
        return ''

access_website('https://www.davita.com/-/media/davita/project/kidneycare/pdf/corporate-governance/dva-pay-equity-disclosure-32119-final.ashx?la=en-us&hash=E5E2F4F69620F3C0BB52FFE818ABCE6CD36BFA12')

But when I do this, I get the following error:

HTTPSConnectionPool(host='www.davita.com', port=443): Max retries exceeded with url: /-/media/davita/project/kidneycare/pdf/corporate-governance/dva-pay-equity-disclosure-32119-final.ashx?la=en-us&hash=E5E2F4F69620F3C0BB52FFE818ABCE6CD36BFA12 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)')))

But I can easily access the site from the browser. All other links that I have do not raise this error.

How can I resolve this? I already tried by providing the certificate, but it still doesn't work

You can set the verify=False argument:

import requests

def access_website(link):
    HEADERS = {
        "User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"
    }
    
    try:
        page = requests.get(link, verify=False, headers=HEADERS)

        return page

    except Exception as x:
        print(x)
        return ""


access_website(
    "https://www.davita.com/-/media/davita/project/kidneycare/pdf/corporate-governance/dva-pay-equity-disclosure-32119-final.ashx?la=en-us&hash=E5E2F4F69620F3C0BB52FFE818ABCE6CD36BFA12"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM