【问题标题】:Pull Major Indices from Yahoo Finance从雅虎财经中提取主要指数
【发布时间】:2021-08-03 16:07:46
【问题描述】:

我想从雅虎财经 (this) 中提取主要世界指数表。

我尝试了以下方法,但没有成功:

library(rvest)
yahoo_windizes <- "https://finance.yahoo.com/world-indices/"
read_html(yahoo_windizes) %>%
    html_nodes("table") %>% 
    html_table()

或者使用htmltab-library:

library(htmltab)
yahoo_windizes %>%
    htmltab(which = 1)

我似乎无法让它工作。非常感谢您的帮助!

【问题讨论】:

  • 如果您还没有听说过它,您可能需要考虑使用tidyquant 包。我上次使用它时,它有索引返回(以及更多)。
  • 感谢 p0bs。我并不特别想要指数回报,我首先想要一份全球主要指数的列表。 tidyquant 是否提供此类信息?
  • 明白。在这种情况下,您可以尝试en.m.wikipedia.org/wiki/List_of_stock_market_indices 进行股权投资。也就是说,鉴于因子投资的兴起,现在的指数比股票多。
  • 感谢 p0bs。我看着那张桌子,想如果可能的话,我想在同一个拉动中使用股票代码。并感谢您的洞察力!

标签: r rvest yahoo-finance


【解决方案1】:

最后 3 个可见列是画布元素。虽然可以获得这些值,但从其他地方提取它们并将它们分配给您的数据对象可能更容易。

同时,如果您只需要前 6 列,您可以通过多种方式重新构建表格,例如通过矩阵:

library(rvest)
library(magrittr)
library(purrr)

yahoo_windizes <- "https://finance.yahoo.com/world-indices/"

table <- read_html(yahoo_windizes) %>%
  html_node("#list-res-table table") 

data <- map(1:6, function(x){ table %>% html_nodes(sprintf('td:nth-of-type(%i)', x)) %>% html_text()}) 

data <- data %>% unlist() %>% matrix(nrow=length(data[[1]]) , ncol=6) %>% as.tibble()

names(data) <- c('Symbol','Name','Last Price', 'Change', '%Change', 'Volume')

【讨论】:

  • 非常感谢!图形会很好,但我想我可以自己制作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 2023-02-22
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多