雅虎财经改变了他们的网站结构。以下 R 代码不再用于抓取目标数据。 我不是检查页面源以查找列表数据存储位置的专家。感谢任何有关更新代码的帮助。
ticker <- "IVV"
url <- paste0("https://finance.yahoo.com/quote/",ticker)
ivv_html <- read_html(url)
node_txt <- ".svelte-tx3nkj" # This contains "table" info of interest
df <- ivv_html %>%
html_nodes(paste0(".container", node_txt)) %>%
map_dfr(~{
tibble(
label = html_nodes(.x, paste0(".label", node_txt)) %>%
html_text(trim = TRUE)
,value = html_nodes(.x, paste0(".value", node_txt)) %>%
html_text(trim = TRUE)
)
})
df %>%
filter(label %in% c("NAV", "PE Ratio (TTM)", "Yield", "Beta (5Y Monthly)", "Expense Ratio (net)"))
从雅虎财经抓取数据的一种简单方法是使用
Quantmod
包,如下所示:
library(quantmod)
getSymbols("EFT",src = "yahoo")
print(EFT)
EFT.Open EFT.High EFT.Low EFT.Close EFT.Volume
2007-01-03 18.44 18.52 18.42 18.52 142000
2007-01-04 18.53 18.69 18.52 18.66 152800
2007-01-05 18.68 18.74 18.64 18.69 103200
2007-01-08 18.70 18.80 18.67 18.78 104600
2007-01-09 18.79 18.85 18.75 18.85 119600
2007-01-10 18.86 18.88 18.80 18.86 115800
2007-01-11 18.88 18.98 18.88 18.96 192400
2007-01-12 18.97 19.02 18.93 19.02 153900
2007-01-16 19.00 19.04 18.90 19.04 141500
2007-01-17 19.03 19.10 18.98 19.05 108100
希望这有帮助!