[英]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.