从雅虎财经下载错误

Posted

技术标签:

【中文标题】从雅虎财经下载错误【英文标题】:Error download from Yahoo finance 【发布时间】:2017-05-20 09:10:24 【问题描述】:

2017 年 5 月 16 日,yahoo.finance 更改网址以下载 Eod 价格。我尝试使用新的 Url,但它不起作用。

With ActiveSheet.QueryTables.Add(Connection:= _
       "TEXT;https://query1.finance.yahoo.com/v7/finance/download/APPL?period1=946854000&period2=1495234800&interval=1d&events=history&crumb=" + mycrumb  _
       , Destination:=Range("Dati!$A$2"))
       .Name = "Data Table"
       .FieldNames = True
       .RowNumbers = False
       .FillAdjacentFormulas = False
       .PreserveFormatting = True
       .RefreshOnFileOpen = False
       .RefreshStyle = xlInsertDeleteCells
       .SavePassword = False
       .SaveData = True
       .AdjustColumnWidth = True
       .RefreshPeriod = 0
       .TextFilePromptOnRefresh = False
       .TextFilePlatform = 850
       .TextFileStartRow = 1
       .TextFileParseType = xlDelimited
       .TextFileTextQualifier = xlTextQualifierDoubleQuote
       .TextFileConsecutiveDelimiter = False
       .TextFileTabDelimiter = True
       .TextFileSemicolonDelimiter = False
       .TextFileCommaDelimiter = True
       .TextFileSpaceDelimiter = False
       .TextFileColumnDataTypes = Array(5, 1, 1, 1, 1, 1, 9)
       .TextFileTrailingMinusNumbers = True
       .Refresh BackgroundQuery:=False

有人可以帮我解决我的问题吗?有人有同样的问题吗? 非常感谢

安德烈亚

【问题讨论】:

"...但它不起作用。"什么不工作?该站点无法访问,您收到 VBA 错误,数据加载但数据不正确,等等......? 我建议现在使用 Alpha Vantage 股票数据 API。它真的很好用。我刚刚写了一篇关于它的博客文章:the-data-wrangler.com/… 【参考方案1】:

首先,旧的雅虎财经 iChart 下载已经一去不复返了。在其中一个论坛帖子中,雅虎员工已确认免费 EOD 数据已被终止,并且不会重新引入。看看这个thread 并寻找尼克松的回复。雅虎最近被威瑞森收购,一定是新方向。

但是,如果您查看 Yahoo 财务页面,CSV 下载链接可以正常工作,但现在有所不同。它是通过一个新的 API 使用的身份验证令牌“crumb”,当您访问该页面时,该 API 链接到一个 cookie。

您的代码正在使用这个新 API,因此您必须获得正确匹配的 cookie 和 crumb 对。我已经通过这个新的 API 整理了一些快速的 Python3 代码来执行此操作(并下载与以前相同的 CSV)。请查看 GitHub 获取源代码yahoo_quote_download。

【讨论】:

你知道从vba下载数据的方法吗?或获得vba的面包屑?谢谢 对不起,您必须自己采用 VBA 的代码。我认为它应该基于相同的原理工作。【参考方案2】:

自 5 月以来,我一直遇到同样的问题。昨天想出了这个代码:

Sub YahooHistDataHoriztl()
 '---enter in cell A1 for daily:   _https://finance.yahoo.com/quote/AAPL/history?interval=1d
 '---enter in cell A1 for weekly:  _https://finance.yahoo.com/quote/AAPL/history?interval=1wk 
 '---enter in cell A1 for DOW weekly:  _https://finance.yahoo.com/quote/^DJI/history?interval=1wk

 '--- Historical Data will load in columns D through J
 '---This program developed after watching video from 'Automate the Web' 2017-07-03
 '---Use Excel & VBA to automate Internet Explorer -beginner
 '----_https://www.youtube.com/watch?v=GRuzoI_kihI&index=3&list=PL0bsMa4HWk9synJWmuqZmI4Z0a3eurN7V
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As htmlLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable used as a counter for Rows
Dim z As Integer 'integer variable used as a counter for Columns
Dim result As String 'string variable that will hold our result link
Dim Pather As String
Dim CurrActiveBook As String
Dim CurrActiveSheet As String
Dim drV As String
Dim targetBook As String

 'dimension (declare or set aside memory for) our variables

Application.DisplayAlerts = False

  Pather = ThisWorkbook.Path & "\"   '---holds current path---
  ChDrive Pather                      '---sets excel to the current drive---
  ChDir Pather                       '---sets excel to the current drive---
  drV = Left(Pather, 3)
  CurrActiveBook = ActiveWorkbook.Name
  CurrActiveSheet = ActiveSheet.Name
   targetBook = ActiveWorkbook.Name
   Range("D2:K62").Select
    Selection.ClearContents         '---clears old data

   Set objIE = New InternetExplorer      'initiating a new instance of Internet Explorer and asigning it to objIE
    objIE.Visible = False      'make IE browser visible (False would allow IE to run in the background)
    objIE.navigate Sheets(CurrActiveSheet).Range("A1").Value      'navigate IE to this web page (a pretty neat search engine really)
     Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop     'wait here a few seconds while the browser is busy
      Application.Wait Now + TimeValue("0:00:05")  '---Give an extra 5 seconds to upload

  Range("D1") = "Date"
  Range("E1") = "Open"
  Range("F1") = "High"
  Range("G1") = "Low"
  Range("H1") = "Close"
  Range("I1") = "Adj Close*"
  Range("J1") = "Avg Vol"

  Range("D2").Select
    y = 0   'the first search result will go in row 2
    z = 0
   For Each aEle In objIE.document.getElementsByClassName("Py(10px)")   'for each <a> element in the collection of objects with class of 'result__a'...
      ActiveCell.Offset(y, z).Value = aEle.innerText
        Debug.Print aEle.innerText

 If z > 5 Then   '---Data loads in one column.  This converts to a table.-------
    z = 0
    y = y + 1   'increment our row counter, so the next result goes below
   Application.Wait Now + (TimeValue("0:00:01") / 2) '--giver data an extra 1/2 second to load up---
Else
    If z = 1 Then
     If Right(ActiveCell.Offset(y, z), 5) = "Split" Then   '---Split reset back to date column---
        z = 0
        y = y + 1
         Else
           If Right(ActiveCell.Offset(y, z), 8) = "Dividend" Then   '---Dividend reset back to date column---
            z = 0
            y = y + 1
           Else
         z = z + 1
        End If
      End If
    Else
      z = z + 1
    End If
  End If
   DoEvents
  Next  'repeat times the # of ele's we have in the collection

objIE.Quit      'close the browser
Columns("D:J").Select
 Selection.Columns.AutoFit
  Range("D1").Select

      Application.DisplayAlerts = True
     MsgBox "Done"
   End Sub '---End of program---

【讨论】:

不清楚这段代码是否是一个解决方案,或者你在说,你有同样的问题。在第一种情况下更新您的答案,在第二种情况下 - 提出一个单独的问题。

以上是关于从雅虎财经下载错误的主要内容,如果未能解决你的问题,请参考以下文章

如何从雅虎财经这样的网站获取数据? [关闭]

用 Python 通过雅虎财经获取股票数据

pandas_datareader下载雅虎财经股价数据

雅虎财经下载数据

雅虎财经 Python API JSON 错误?

雅虎财经 API 错误 400 在亚洲