如何使用RSelenium从网页下载嵌入的PDF文件?

时间:2021-05-15 20:11:06

标签: r rvest rselenium

编辑:根据我目前收到的评论,我设法使用 RSelenium 访问我正在寻找的 PDF 文件,使用以下代码:

library(RSelenium)
driver <- rsDriver(browser = "firefox")
remote_driver <- driver[["client"]]
remote_driver$navigate("https://www.rad.cvm.gov.br/enetconsulta/frmGerenciaPaginaFRE.aspx?CodigoTipoInstituicao=1&NumeroSequencialDocumento=62398")
# It needs some time to load the page
option <- remote_driver$findElement(using = 'xpath', "//select[@id='cmbGrupo']/option[@value='PDF|412']")
option$clickElement()

现在,我需要 R 来单击下载按钮,但我无法做到。我试过了:

button <- remote_driver$findElement(using = "xpath", "//*[@id='download']")
button$clickElement()

但我收到以下错误:

Selenium message:Unable to locate element: //*[@id="download"]
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10'

Erro:    Summary: NoSuchElement
 Detail: An element could not be located on the page using the given search parameters.
 class: org.openqa.selenium.NoSuchElementException
 Further Details: run errorDetails method

有人能说出这里有什么问题吗? 谢谢!

原始问题:

我有几个网页需要从中下载嵌入的 PDF 文件,我正在寻找一种使用 R 自动化的方法。这是其中一个网页:https://www.rad.cvm.gov.br/enetconsulta/frmGerenciaPaginaFRE.aspx?CodigoTipoInstituicao=1&NumeroSequencialDocumento=62398 这是来自 CVM(Comissãode Valores Mobiliários,巴西相当于美国证券交易委员会 - SEC)的网页,可从以下网址下载财务报表附注 (Notas Explicativas)巴西公司。

我尝试了多种选择,但该网站的构建方式似乎难以提取直接链接。 我尝试了此处 Downloading all PDFs from URL 中的建议,但 html_nodes(".ms-vb2 a") %>% html_attr("href") 产生了一个空字符向量。 类似地,当我在这里尝试使用 https://www.samuelworkman.org/blog/scraping-up-bits-of-helpfulness/ 中的方法时,html_attr("href") 会生成一个空向量。

我不习惯用 R 网络抓取代码,所以我无法弄清楚发生了什么。 感谢您的帮助!

0 个答案:

没有答案
相关问题