r - R 从 Yahoo Finance 抓取 HTML 表
问题描述
我想从雅虎财经中抓取一张表格并将其下载为数据框。不幸的是,我真的不知道如何使用rvest
-package 来做到这一点。
这是第一种方法:
library(tidyverse)
library(rvest)
url<-"https://finance.yahoo.com/calendar/ipo?from=2021-02-21&to=2021-02-27&day=2021-02-23"
url %>%
html() %>%
html_nodes(xpath="table") %>%
html_table()
正如预期的那样,代码不起作用。有人能帮我吗?
我想将框架表作为数据框:
提前谢谢了!
解决方案
不幸的是,使用html_table
. 这是一种从表中提取单个值并进行一些后处理以获取数据框中的数据的方法。
library(rvest)
url<-"https://finance.yahoo.com/calendar/ipo?from=2021-02-21&to=2021-02-27&day=2021-02-23"
url %>%
read_html() %>%
html_nodes('table') %>%
.[[1]] -> tab1
header <- tab1 %>% html_nodes('th') %>% html_text()
result <- tab1%>%
html_nodes('tr.simpTblRow td') %>%
html_text() %>%
matrix(ncol = 9, byrow = TRUE) %>%
as.data.frame()
names(result) <- header
result
# Symbol Company Exchange
#1 VELOU Velocity Acquisition Corp. Units Nasdaq
#2 FTAAU FTAC Athena Acquisition Corp. Unit Nasdaq
#3 CMIIU CM Life Sciences II Inc. Unit Nasdaq
#4 Metropress Ltd LSE
#5 CTWO.P.V County Capital 2 Ltd TSXV
#6 GSEVU Gores Holdings VII, Inc. Units Nasdaq
#7 NVOS Novo Integrated Sciences, Inc. Common Stock Nasdaq
#8 SLAMU Slam Corp. Unit Nasdaq
# Date Price Range Price Currency Shares Actions
#1 Feb 23, 2021 10.00 - 10.00 - USD - Expected
#2 Feb 23, 2021 - - USD - Expected
#3 Feb 23, 2021 10.00 - 10.00 - USD - Expected
#4 Feb 01, 2021 - 6 GBP 45452752 Priced
#5 Nov 19, 2020 0.08 - 0.08 0.1 CAD 6000000 Priced
#6 Feb 23, 2021 - - USD - Expected
#7 Feb 23, 2021 - - USD - Expected
#8 Feb 23, 2021 10.00 - 10.00 - USD - Expected
推荐阅读
- javascript - 循环中最后一项的jquery计算计算两次
- android - 在 Firebase 云消息传递中正确使用令牌
- android - Android Recyclerview 停止加载已加载的项目
- nginx - 如果省略了文件夹的斜杠,则重定向到反向代理后面的错误 url
- android - Toast 未在 Android Q 中显示
- javascript - 当用户位于页面底部时编辑 css 值
- aws-lambda - 上传 lambda 时出错“无法将项目导出到 jar”
- python - 在 Pandas 中,用逗号将一列中的单元格拆分为多行
- .net - 我可以在我的开发环境中使用特定配置而不更改 appsettings.json 吗?
- android - 适用于 Android 的 Google Places API 未显示