无法使用Quantum in R从Yahoo Finance下载数据

我正尝试使用下面的代码从雅虎下载数据:

library(quantmod) getSymbols("WOW", auto.assign=F) 

除了现在,在我的小组任务到期之前的5天之前,这一切对我来说都是有效的。

除了现在我收到这个错误:

 Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, : cannot download all files In addition: Warning message: In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, : URL 'https://ichart.finance.yahoo.com/table.csv? s=WOW&a=0&b=01&c=2007&d=4&e=17&f=2017&g=d&q=q&y=0&z=WOW&x=.csv': status was '502 Bad Gateway' 

价格历史CSV CSV的似乎已经改变

旧版https://chart.finance.yahoo.com/table.csv?s=AAPL&a=2&b=17&c=2017&d=3&e=17&f=2017&g=d&ignore=.csv

新build: https : //query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1492438581&period2=1495030581&interval=1d&events=history&crumb=XXXXXXX

新版本附加了一个“crumb”字段,该字段似乎反映了用户浏览器中的cookie信息。 似乎他们故意阻止价格历史的自动下载,并强制查询提供信息在Web浏览器中validationCookie

解决方法详见https://github.com/joshuaulrich/quantmod/issues/157

Essentialy

 remotes::install_github("joshuaulrich/quantmod", ref="157_yahoo_502") # or devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502") 

我一直在想,为什么雅虎能够提供数据下载,以及如果他们停止了这样的操作,我会变得如此糟糕。 幸运的是,在约书亚·乌尔里希(Joshua Ulrich)的指导下,

现在可能是多余的,我编写了一个修复程序,显示了解决下载问题的一种方法。

 library(xts) getSymbols.yahoo.fix <- function (symbol, from = "2007-01-01", to = Sys.Date(), period = c("daily","weekly","monthly"), envir = globalenv(), crumb = "YourCrumb", DLdir = "~/Downloads/") { #1 # build yahoo query query1 <- paste("https://query1.finance.yahoo.com/v7/finance/download/",symbol,"?",sep="") fromPosix <- as.numeric(as.POSIXlt(from)) toPosix <- as.numeric(as.POSIXlt(to)) query2 <- paste("period1=", fromPosix, "&period2=", toPosix, sep = "") interval <- switch(period[1], daily = "1d", weekly = "1wk", monthly = "1mo") query3 <- paste("&interval=", interval, "&events=history&crumb=", crumb, sep = "") yahooURL <- paste(query1, query2, query3, sep = "") #' requires browser to be open utils::browseURL("https://www.google.com") #' run the query - downloads the security as a csv file #' DLdir defaults to download directory in browser preferences utils::browseURL(yahooURL) #' wait 500 msec for download to complete - mileage may vary Sys.sleep(time = 0.5) yahooCSV <- paste(DLdir, symbol, ".csv", sep = "") yahooDF <- utils::read.csv(yahooCSV, header = TRUE) #' ------- #' if you get: Error in file(file, "rt") : cannot open the connection #' it's because the csv file has not completed downloading #' try increasing the time for Sys.sleep(time = x) #' ------- #' delete the csv file file.remove(yahooCSV) # convert date as character to date format yahooDF$Date <- as.Date(yahooDF$Date) # convert to xts yahoo.xts <- xts(yahooDF[,-1],order.by=yahooDF$Date) # assign the xts file to the specified environment # default is globalenv() assign(symbol, yahoo.xts, envir = as.environment(envir)) print(symbol) } #1 

它是这样工作的:

  • 转到https://finance.yahoo.com/quote/AAPL/history?p=AAPL
  • 右键点击“下载数据”并复制链接
  • 在“&crumb =”之后复制面包屑,并在函数调用中使用它
  • 将DLdir设置为浏览器首选项中的默认下载目录
  • 设置envir = as.environment(“yourEnvir”) – 默认为globalenv()
  • 下载后,csv文件将从您的下载目录中删除,以避免混乱
  • 请注意,这将在浏览器中打开“未命名”窗口
  • 作为一个简单的testing:getSymbols.yahoo.fix(“AAPL”)

您也可以使用lapply的getSymbols.yahoo.fix来获取资产数据的列表

 from <- "2016-04-01" to <- Sys.Date() period <- "daily" envir <- globalenv() crumb <- "yourCrumb" DLdir <- "~/Downloads/" assetList <- c("AAPL", "ADBE", "AMAT") lapply(assetList, getSymbols.yahoo.fix, from, to, envir = globalenv(), crumb = crumb, DLdir)} 

在Mac OS X 10.11的RStudio中使用Safari作为默认浏览器进行编码。 它也似乎与Chrome一起工作,但是您需要使用Chrome的cookie crumb。 我使用cookie拦截器,但必须将finance.yahoo.com列入白名单才能保留未来浏览器会话的cookie。

getSymbols.yahoo.fix可能是有用的。 qauantmod :: getSymbols的必要性,有更多的代码内置的选项和exception处理。 我正在为个人工作编码,所以我经常从包函数中提取我需要的代码段。 我没有对getSymbols.yahoo.fix进行基准testing,因为当然,我没有用于比较的GetSymbol工作版本。 此外,我不能放弃进入我的第一个stackoverflow答案的机会。

quantmod 0.4-9版本修复了这个问题,现在在CRAN上可用。

我也遇到这个错误。 mrexcel fourm(jonathanwang003)上的用户解释说,新的URL使用Unix时间码进行date。 更新后的VBA代码如下所示:

 qurl = "https://query1.finance.yahoo.com/v7/finance/download/" & Symbol qurl = qurl & "?period1=" & (StartDate - DateSerial(1970, 1, 1)) * 86400 & _ "&period2=" & (EndDate - DateSerial(1970, 1, 1)) * 86400 & _ "&interval=1d&events=history&crumb=" & **Crumb** QueryQuote: With Sheets(Symbol).QueryTables.Add(Connection:="URL;" & qurl, Destination:=Sheets(Symbol).Range("a1")) .BackgroundQuery = True .TablesOnlyFromHTML = False .Refresh BackgroundQuery:=False .SaveData = True End With 

这里缺less的部分是如何从浏览器中检索包含cookie信息的“Crumb”字段。 任何人有任何想法。 我发现这个post,这可能有所帮助: https : //www.mrexcel.com/forum/excel-questions/1001259-when-using-querytables-what-posttext-syntax-click-button-webpage.html (看看最后由john_w发布)。