当我尝试使用巴西政府网页上的xml2::read_html()
功能时,收到SSL证书错误。当我尝试访问
page = xml2::read_html("https://www.gov.br/planalto/pt-br/acompanhe-o-planalto/discursos")
我收到以下错误:
Error in open.connection(x, "rb") :
SSL certificate problem: unable to get local issuer certificate
我发现了另外一个具有3种可能解决方案的问题:
httr::set_config(config(ssl_verifypeer = 0L)) #1
httr::set_config(config(ssl_verifypeer = FALSE)) #2
Sys.setenv(LIBCURL_BUILD="winssl") #3
他们都没有解决我的问题,然后我尝试在Kaggle笔记本上运行代码,并且收到了相同的错误消息,所以我可以看到问题不在我的PC上。
来自https://curl.haxx.se/docs/sslcerts.html:
证书验证
...
- 告诉libcurl不验证对等方。使用libcurl可以使用curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,FALSE)禁用此功能;
使用curl命令行工具,使用-k /-insecure禁用它。
因此,从命令行(或终端)执行以下操作:
curl -k https://www.gov.br/planalto/pt-br/acompanhe-o-planalto/discursos
以下内容作为解决方法,也可以使用(使用curl
库):
url <- "https://www.gov.br/planalto/pt-br/acompanhe-o-planalto/discursos"
curl::handle_setopt(h, ssl_verifyhost = 0, ssl_verifypeer=0)
curl::curl_download(url=url, destfile = "file_test.html", handle = h)
我找不到在xml2软件包选项中设置不安全选项的方法,这将是对该问题的正确答案。
虽然,有趣的是,以下内容也“有效”,但仅用于下载html文件,直接对其进行解析,没有运气。
curl::handle_setopt(h, ssl_verifyhost = 0, ssl_verifypeer=0)
xml2::download_html(url, handle = h)
xml2::read_xml(url, handle = h) #doesnt work
xml2::read_html(url, handle = h) #doesnt work
实际上,遵循信息here,选项181
#> 181 ssl_verifypeer CURLOPT_SSL_VERIFYPEER integer
应该是您尝试过但没有起作用的。可能是一个错误,因为它是从命令行使用的相同选项。