有没有办法在不使用 RSelenium 的情况下从“网站按钮点击”下载 CSV 文件?

问题描述

链接是:http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/fundos-de-investimentos/fii/fiis-listados/

我必须点击“Exportar lista completa de Fundos em CSV”按钮才能下载文件

出于某种原因,我在 R 中读取页面的唯一方法是使用

url = "http://www.b3.com.br/pt_br/produtos-e-servicos/negociacao/renda-variavel/fundos-de-investimentos/fii/fiis-listados/"
download.file(url1,destfile = "scrapedpage.html",quiet=TRUE)

content <- read_html("scrapedpage.html")

解决方法

“Exportar lista completa de Fundos em CSV”按钮来自不同的来源,如果您在 chrome 上打开开发工具,您可以看到整个正文在 iframe 中,其来源为 https://sistemaswebb3-listados.b3.com.br/fundsPage/7

我建议你

  1. 先获取 iframe 源代码
  2. 抓取它以找到 btn 的链接。

对于这种情况,它是 https://sistemaswebb3-listados.b3.com.br/fundsProxy/fundsCall/GetListFundDownload/eyJ0eXBlRnVuZCI6NywicGFnZU51bWJlciI6MSwicGFnZVNpemUiOjIwfQ== 这将 csv 数据您所要做的就是将其转换为 csv。

由于您可以轻松地从网站源中获取这些内容,因此您可以使用 cheerio 从 HTML 中快速抓取并抓取该项目