在R中,获取请求可以正常工作,但在Python中却无法正常工作(证书验证失败错误)

2024-09-30 22:22:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我通常在R中进行web抓取,但现在我必须在python中进行。我在第一个台阶上堆叠。在r中,正常的GET请求与往常一样工作:

library(rvest)
p <- read_html("https://www.belex.rs/")

在python中,我得到一个错误:

import requests
hdrs = {'User-Agent': 'Mozilla / 5.0 (X11 Linux x86_64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 52.0.2743.116 Safari / 537.36'}
p = requests.get("https://www.belex.rs/", allow_redirects=True, headers=hdrs)

"...requests.exceptions.SSLError: HTTPSConnectionPool(host='www.belex.rs', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))"

为什么GET请求在R中工作,但在python中不工作,以及如何使其工作(我知道有verify=False选项,但我了解到由于生产中的安全问题,不建议使用它)


Tags: httpswebreadgetwwwlibrarycertificaterequests