首页 > 解决方案 > 在 R 中抓取数据

问题描述

有没有办法在 R 中废弃数据:

本网站的一般信息/发布日期:https ://www.euronext.com/en/products/etfs/LU1437018838-XAMS/market-information

到目前为止,我已经使用了这段代码,但是生成的 XML 文件不包含我需要的信息:

library(rvest)
library(XML)

url <- paste("https://www.euronext.com/en/products/etfs/LU1437018838-XAMS/market-information",sep="")

download.file(url, destfile = "scrapedpage.html", quiet=TRUE)
content <- read_html("scrapedpage.html")

content1 <- htmlTreeParse(content, error=function(...){}, useInternalNodes = TRUE)

标签: rweb-scraping

解决方案


你试图废弃的是一个名为 factsheet 的 AJAX 对象(我不知道 javascript,所以我不能告诉你更多)。这是获得所需内容的解决方案:使用浏览器中的网络分析(XHR 事物)获取 javascript 使用的数据的 URL。见这里

library(rvest)
url <- read_html("https://www.euronext.com/en/factsheet-ajax?instrument_id=LU1437018838-XAMS&instrument_type=etfs")
launch_date <- url %>% html_nodes(xpath = "/html/body/div[2]/div[1]/div[3]/div[4]/strong")%>%
  html_text()

推荐阅读