使用 R Studio 从 Investment.com 抓取 STOXX EUROPE 600 指数的成分名称

问题描述

我正在尝试从以下网站自动读取 STOXX 600 EUROPE 的组件名称https://nl.investing.com/indices/stoxx-600-components。 看下面的代码

Library(rvest)
Library(tidyverse)
URL <-  "https://nl.investing.com/indices/stoxx-600-components"
publications <-read_html(URL) %>%
#defined the Node
html_nodes("leftColumn") %>% 
html_text() %>% data.frame()

但是出了点问题,因为我看到了一个包含 0 个观察值的数据集。有人可以帮助我如何使用 R studio 从 STOXX EUROPE 600 指数中抓取名称

解决方法

利用 html_table 和表的 id (cr1),您可以获得一个包含名称和价格信息的数据框,如下所示:

library(rvest)
library(tidyverse)
url <-  "https://nl.investing.com/indices/stoxx-600-components"
html <-read_html(url) 

html %>%
  html_node("#cr1") %>% 
  html_table() %>% 
  head()
#> # A tibble: 6 x 10
#>   ``    Naam     Laatst   Hoog     Laag     `+/-`  `+/- %` Vol.    Tijd    ``   
#>   <lgl> <chr>    <chr>    <chr>    <chr>    <chr>  <chr>   <chr>   <chr>   <lgl>
#> 1 NA    3I Group 1.244,00 1.258,00 1.234,50 -11,50 -0,92%  774,92K 17:35:… NA   
#> 2 NA    A2A      1,706    1,722    1,694    -0,007 -0,41%  12,37M  17:35:… NA   
#> 3 NA    AAK      192,8    193,2    191,1    +0,5   +0,23%  246,89K 17:29:… NA   
#> 4 NA    Aalberts 46,93    47,15    46,46    -0,23  -0,49%  66,32K  17:36:… NA   
#> 5 NA    ABB      31,34    31,38    30,98    +0,04  +0,13%  2,71M   17:31:… NA   
#> 6 NA    ABN AMRO 11,29    11,37    11,18    +0,06  +0,52%  2,43M   17:35:… NA