我使用R包quantmod没有问题,它使用Yahoo获取股票数据,如下所示:
get_stock_prices <- function(target, return_format = "tibble", ...) {
# Get stock prices
print(target)
stock_prices_xts <- getSymbols(Symbols = target, auto.assign = FALSE, ...)
# Rename
names(stock_prices_xts) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")
# Return in xts format if tibble is not specified
if (return_format == "tibble") {
stock_prices <- stock_prices_xts %>%
as_tibble() %>%
rownames_to_column(var = "Date") %>%
mutate(Date = ymd(Date))
} else {
stock_prices <- stock_prices_xts
}
write.csv(stock_prices, file = paste(target, "csv", sep = '.'))
}
我只知道pandas_datareader在Python中实现了类似的功能。不幸的是,随着yahoo和google API的改变,这个包被破坏了。这段代码:
import pandas_datareader as pdr
panel_data = pdr.get_data_yahoo('MSFT')
结果如下:
Yahoo Actions has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.
有没有一个目前正在运行的Python包来实现上述功能。我知道quandl,但这是一项付费服务。谢谢。
发布于 2018-07-30 17:17:03
试试fix_yahoo_finance
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
data = yf.download("MSFT", start="2017-01-01", end="2017-04-30")
print(data)
[*********************100%***********************] 1 of 1 downloaded
Open High ... Adj Close Volume
Date ...
2017-01-03 62.790001 62.840000 ... 60.664047 20694100
2017-01-04 62.480000 62.750000 ... 60.392612 21340000
2017-01-05 62.189999 62.660000 ... 60.392612 24876000
2017-01-06 62.299999 63.150002 ... 60.916084 19922900
2017-01-09 62.759998 63.080002 ... 60.722206 20256600
2017-01-10 62.730000 63.070000 ... 60.702820 18593000
https://stackoverflow.com/questions/51459752
复制相似问题