R 中的函数 html_table() 没有给我完整的表格

问题描述 投票:0回答:1

我想通过网络抓取过去五年的历史股市数据。但我的代码只给了我 100 行的小标题,但网站上的表格要长得多。你知道我哪里错了吗?

library("rvest")
library("dplyr")

  url <- "https://finance.yahoo.com/quote/BAC/history?period1=1540512000&period2=1698278400& interval=1d&filter=history&frequency=1d&includeAdjustedClose=true"
  dt <- read_html(url)
  dt %>% html_table(fill = T) 
r web-scraping html-table rvest yahoo-finance
1个回答
0
投票

您可以使用

tidyquant
从 Yahoo 获取证券价格

library(tidyverse)
library(tidyquant)

tq_get(x = "BAC") %>% 
  arrange(desc(date))

# A tibble: 2,725 × 8
   symbol date        open  high   low close   volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
 1 BAC    2023-10-27  26.1  26.1  25.0  25.2 64614700     25.2
 2 BAC    2023-10-26  25.5  26.4  25.4  26.1 60921000     26.1
 3 BAC    2023-10-25  25.4  25.6  25.2  25.5 45522400     25.5
 4 BAC    2023-10-24  25.7  25.9  25.4  25.5 55975700     25.5
 5 BAC    2023-10-23  26.1  26.2  25.5  25.6 59857200     25.6
 6 BAC    2023-10-20  26.8  26.9  26.2  26.3 62029800     26.3
 7 BAC    2023-10-19  27.2  27.8  26.9  27.0 58611000     27.0
 8 BAC    2023-10-18  27.5  28.0  27.2  27.3 68371100     27.3
 9 BAC    2023-10-17  27.0  27.9  26.7  27.6 95344200     27.6
10 BAC    2023-10-16  27.2  27.2  26.8  27.0 56817500     27.0
# ℹ 2,715 more rows
# ℹ Use `print(n = ...)` to see more rows

© www.soinside.com 2019 - 2024. All rights reserved.