- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有兴趣使用 R 分析来自 Yahoo Finance 的多个股票代码的余额、收入和现金流量表。
我看到有从雅虎财经提取信息的 R 包,但我看到的所有示例都涉及历史股票价格信息。有没有办法可以使用 R 从这些语句中提取历史信息?
例如,对于 Apple (AAPL),可检索链接如下:
本质上,目标是创建三个数据帧(AAPL_cashflow
、AAPL_income
和 AAPL_balance
),它们的模式与网站。每行由财务类型标识,列是日期。
有人有解析和抓取表格的经验吗?我认为 rvest
可以帮助解决这个问题,对吧?
提前致谢!
最佳答案
使用 tidyverse
中的一些软件包,这应该可以帮助您入门:
library(tidyverse)
library(rvest)
"https://finance.yahoo.com/quote/AAPL/financials?p=AAPL" %>%
read_html() %>%
html_table() %>%
map_df(bind_cols) %>%
as_tibble()
# A tibble: 28 x 5
X1 X2 X3 X4 X5
<chr> <chr> <chr> <chr> <chr>
1 Revenue 9/30/2017 9/24/2016 9/26/2015 9/27/20…
2 Total Revenue 229,234,000 215,639,000 233,715,000 182,795…
3 Cost of Revenue 141,048,000 131,376,000 140,089,000 112,258…
4 Gross Profit 88,186,000 84,263,000 93,626,000 70,537,…
5 Operating Expenses Operating Expenses Operating Expenses Operating Expenses Operati…
6 Research Development 11,581,000 10,045,000 8,067,000 6,041,0…
7 Selling General and Administrative 15,261,000 14,194,000 14,329,000 11,993,…
8 Non Recurring - - - -
9 Others - - - -
10 Total Operating Expenses 167,890,000 155,615,000 162,485,000 130,292…
# ... with 18 more rows
请注意,如果您想获取第一行并将其视为列名,请将 header = TRUE
添加到 html_table
调用中。例如,这将为您提供日期作为 finances
数据框中的列名。
此外,此数据框内有多个表,因此您需要对其进行 reshape 才能使用数据。例如,var X2
到 X5
当前应该是数字类型时是字符。
一个例子可能是:
finances <- "https://finance.yahoo.com/quote/AAPL/financials?p=AAPL" %>%
read_html() %>%
html_table(header = TRUE) %>%
map_df(bind_cols) %>%
as_tibble()
finances %>%
mutate_all(funs(str_replace_all(., ",", ""))) %>%
mutate_all(funs(str_replace(., "-", NA_character_))) %>%
mutate_at(vars(-Revenue), funs(str_remove_all(., "[a-zA-Z]"))) %>%
mutate_at(vars(-Revenue), funs(as.numeric)) %>%
drop_na()
# A tibble: 14 x 5
Revenue `9/30/2017` `9/24/2016` `9/26/2015` `9/27/2014`
<chr> <dbl> <dbl> <dbl> <dbl>
1 Total Revenue 229234000. 215639000. 233715000. 182795000.
2 Cost of Revenue 141048000. 131376000. 140089000. 112258000.
3 Gross Profit 88186000. 84263000. 93626000. 70537000.
4 Research Development 11581000. 10045000. 8067000. 6041000.
5 Selling General and Administrative 15261000. 14194000. 14329000. 11993000.
6 Total Operating Expenses 167890000. 155615000. 162485000. 130292000.
7 Operating Income or Loss 61344000. 60024000. 71230000. 52503000.
8 Total Other Income/Expenses Net 2745000. 1348000. 1285000. 980000.
9 Earnings Before Interest and Taxes 61344000. 60024000. 71230000. 52503000.
10 Income Before Tax 64089000. 61372000. 72515000. 53483000.
11 Income Tax Expense 15738000. 15685000. 19121000. 13973000.
12 Net Income From Continuing Ops 48351000. 45687000. 53394000. 39510000.
13 Net Income 48351000. 45687000. 53394000. 39510000.
14 Net Income Applicable To Common Shares 48351000. 45687000. 53394000. 39510000.
我们可以更进一步,使用 gather
使数据框更“整洁”:
finances %>%
mutate_all(funs(str_replace_all(., ",", ""))) %>%
mutate_all(funs(str_replace(., "-", NA_character_))) %>%
mutate_at(vars(-Revenue), funs(str_remove_all(., "[a-zA-Z]"))) %>%
mutate_at(vars(-Revenue), funs(as.numeric)) %>%
drop_na() %>%
gather(key = "date", value, -Revenue) %>%
mutate(date = lubridate::mdy(date)) %>%
rename("var" = Revenue) %>%
as_tibble()
# A tibble: 56 x 3
var date value
<chr> <date> <dbl>
1 Total Revenue 2017-09-30 229234000.
2 Cost of Revenue 2017-09-30 141048000.
3 Gross Profit 2017-09-30 88186000.
4 Research Development 2017-09-30 11581000.
5 Selling General and Administrative 2017-09-30 15261000.
6 Total Operating Expenses 2017-09-30 167890000.
7 Operating Income or Loss 2017-09-30 61344000.
8 Total Other Income/Expenses Net 2017-09-30 2745000.
9 Earnings Before Interest and Taxes 2017-09-30 61344000.
10 Income Before Tax 2017-09-30 64089000.
# ... with 46 more rows
关于r - 如何使用 R 从 Yahoo Finance 抓取财务数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49845173/
我尝试进行投资组合优化,但随后出现了TypeError: minimize_sharpe() missing 1 required positional argument: 'log_returns'
考虑到 future 的速度读取和存档的全局大小,存储报价数据(开盘价、最高价、最低价、收盘价、成交量)的最有效方法是什么? XML ? But It is resource consumption
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我正在尝试从 Yahoo! 检索市场数据财务和脚本多年来一直运行良好,但最近,它停止显示道琼斯数据。这是网址: http://download.finance.yahoo.com/d/quotes.c
因此,当我使用组合图表(条形图 + 折线图)时,我对以前版本的 Chartjs(即 2.5)遇到了一些问题,并且在处理时间序列数据时遇到了困难。 就上下文而言,我使用的是财务数据(股票价格和交易量)。
我是一名优秀的程序员,十分优秀!