简体   繁体   English

为什么我不能使用请求库访问网站,而我仍然可以从浏览器访问它?

[英]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.我有这个 function 来访问网站。

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:您可以设置verify=False参数:

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"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能使用Python“请求”库填写此表单? - Why I can't fill this form using Python “requests” library? 我可以将私人浏览器与 Python 请求一起使用吗 - Can I use Private Browser with Python Requests 为什么我无法通过 urllib 访问 Digikey 的网站? - Why can't I access Digikey's website through urllib? 为什么请求无法访问此网站上的安全页面? - Why requests can't access the secured page on this website? 我可以使用 Python 请求库发送不一致的请求吗 - Can I use Python requests library to send inconsistent requests 为什么我不能摆脱这个 get requests 循环? - Why can't I break from this get requests loop? 我可以从本地打开图片,但是当我从网站访问时,总是存在一些错误 - I can open picture from local, but when I visit from website there is always present contain some errors 我无法从浏览器访问 scrapyd 端口 6800 - I can't access scrapyd port 6800 from browser 如何在使用 python 请求时截取网站的屏幕截图? - How can i take screenshots of a website while using python requests? 为什么我不能使用 Python 的 `requests` 库在 POST 请求中将 `None` 作为数据发送? - Why can't I send `None` as data in a POST request using Python's `requests` library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM