如何从R中具有“显示更多”属性的网站上抓取文字?

时间:2019-08-18 11:32:56

标签: r

Hello Stack溢出社区,

有一个提供财务数据Investing.com的网站,提供有关某些经济变量的数据点和预测。

对于进一步感兴趣的人,带有我要抓取的信息的确切URL是here

我的目标是删除此信息;但是,显示信息的表的末尾包含“显示更多”属性。当我单击它时,一次出现六个新元素。我正在寻找一种方法,一次可以分解五十个或更多元素,然后将其废弃在R中。

我知道如何从网站上抓取文字,但不知道如何解决“显示更多”属性。

我试图修改网站的Java代码,尽管没有成功。我对Java的了解还很有限。

谢谢您的时间!

1 个答案:

答案 0 :(得分:0)

我想这就是你想要的。

library(rvest)

url <- "https://www.investing.com/economic-calendar/eia-crude-oil-inventories-75"

page <- read_html(url)
nodes <- html_nodes(page, "table") # you can use Selectorgadget to identify the node
table <- html_table(nodes[[1]]) # each element of the nodes list is one table that can be extracted
head(table)

结果:

  Release Date  Time  Actual Forecast Previous   
1 Dec 27, 2019 11:00 -5.474M  -1.724M  -1.085M NA
2 Dec 18, 2019 10:30 -1.085M  -1.288M   0.822M NA
3 Dec 11, 2019 10:30  0.822M  -2.763M  -4.856M NA
4 Dec 04, 2019 10:30 -4.856M  -1.734M   1.572M NA
5 Nov 27, 2019 10:30  1.572M  -0.418M   1.379M NA
6 Nov 20, 2019 10:30  1.379M   1.543M   2.219M NA