【发布时间】:2021-05-15 20:11:06
【问题描述】:
编辑:从我目前收到的 cmets 中,我设法使用 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ão de 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 中使用网页 scraping 代码,所以我无法弄清楚发生了什么。 感谢您的帮助!
【问题讨论】:
-
该页面上没有链接 pdf 文件,因此 pkg-rvest 似乎不太可能成为解决方案。我猜你不习惯用任何语言进行网页抓取。该页面的源代码是用 Javascript 编写的,用于打开可以选择 pdf 文件的对话框窗口的按钮名为“btnGeraRelatorioPDF”。我怀疑您将需要获得一个包,该包具有比 rvest 提供的更多的与网页交互的功能。 Selenium 包曾经是 goto 解决方案,但我认为其他浏览器导航工具也可用。使用浏览器查看页面源代码。
-
使用“[r] javascript dialog”搜索 SO 我发现这个可能有用的答案:stackoverflow.com/questions/29759438/rselenium-popup/… 这是来自同一贡献者的另一个可能有用的答案,@JDHarrison:stackoverflow.com/questions/38156180/…
-
根据@IRTFM 的问题建议,我正在使用 RSelenium,但我无法弄清楚如何在第一个下拉菜单中选择“Notas Explicativas”,它会生成嵌入的我要下载的 PDF 文件。
-
@RonakShah,打开页面后
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")我想在第一个下拉菜单中选择“Notas Explicativas”选项,生成我要下载的pdf文件. -
我想我没有仔细阅读您的注释,并且我之前指定的按钮名称不正确。在我看来,您需要首先选择该菜单带(或条带?不确定正确的术语)本身。在 Chrome 中,在该菜单条上执行“检查”会突出显示以
<select name="cmbGrupo" onchange="javascript:setTimeout('__doPostBack(\'cmbGrupo\',\'\')', 0)" id="cmbGrupo" label="Você esta vendo:" showlabel="true" style="width:49%;">开头的部分,然后在其下方列出选项。