【问题标题】:Excel VBA: get Yahoo Finance data on MacExcel VBA:在 Mac 上获取 Yahoo Finance 数据
【发布时间】:2021-10-07 05:38:03
【问题描述】:

我正在尝试在 Mac 上的 Excel 上从 Yahoo Finance 获取数据。

据我所知,在 Mac 上获取 Web 数据的常用方法是 WebQuery。但是,有时它可以正常工作,有时会针对之前正常工作的同一组代码抛出错误 1004。错误文本: "Microsoft Excel cannot access the file %link%. There are several possible reasons:"

我不知道为什么会这样。唯一的建议是因为 URL 不包含 Yahoo 需要的 cookie / crumb。

出于测试目的,我在 Windows 上使用了WinHttpRequest。它有效 - Excel 成功获取数据。 Mac 上有一个替代方案 - Tim Hall 的WebHelpers。使用这套很棒的工具,我能够在 Mac 上获取 cookie 和 crumb。 但是当我尝试从 Yahoo 下载 CSV 时,response.Content 有这个字符串:{"finance":{"result":null,"error":{"code":"Not Acceptable","description":"HTTP 406 Not Acceptable"}}}

一般来说,我有几个问题:

  1. 有没有办法在 WebQuery 方法中添加 cookie?不过,我不确定这是否有效并有助于规避错误。
  2. 为什么Response 返回错误 406?特别是这段代码sn-p:
     client.BaseUrl = tickerURL
     request.Method = HttpGet
     request.Format = PlainText
     request.AddCookie "Cookie", cookie
     request.AddHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"

     Set response = client.Execute(request)
     resultFromYahoo = response.Content

这是一个代码,用于在 Windows 上使用 WinHttpRequest 或在 Mac 上使用 Tim Hall 的包来接收 Yahoo Finance 数据:

Sub getYahooFinanceData(stockTicker As String, StartDate As String, EndDate As String, frequency As String, cookie As String, crumb As String)
' forked from:
' http://investexcel.net/multiple-stock-quote-downloader-for-excel/
    

    Dim resultFromYahoo As String
    Dim objRequest
    Dim csv_rows() As String
    Dim tickerURL As String


    'Make URL
    tickerURL = "https://query1.finance.yahoo.com/v7/finance/download/" & stockTicker & _
        "?period1=" & StartDate & _
        "&period2=" & EndDate & _
        "&interval=" & frequency & "&events=history" & "&crumb=" & crumb
    '***************************************************
               
         
               
    'Get data from Yahoo
    #If Mac Then

        Dim client As New WebClient
        Dim response As WebResponse
        Dim request As New WebRequest
        
        client.BaseUrl = tickerURL
        request.Method = HttpGet
        request.Format = PlainText
        request.AddCookie "Cookie", cookie
        request.AddHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"

        Set response = client.Execute(request)
        DoEvents
'' ERROR 406 on MAC ''
        If response.StatusCode = Ok Then
            resultFromYahoo = response.Content
        Else
            MsgBox "An error occured while getting data for " & stockTicker & "'", vbInformation
            Exit Sub
        End If
        
        
    #Else
        Set objRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
        With objRequest
            .Open "GET", tickerURL, False
            .SetRequestHeader "Cookie", cookie
            .Send
            .WaitForResponse
            resultFromYahoo = .ResponseText
        End With
    #End If
    

    '***************************************************
    csv_rows() = Split(resultFromYahoo, Chr(10))
  
End Sub

【问题讨论】:

    标签: excel vba web xmlhttprequest


    【解决方案1】:

    终于找到解决办法了!在与 Python 相关的类似主题中找到答案:https://stackoverflow.com/a/68259438/8524164

    简而言之,我们需要修改 user-agent 和其他请求参数来模拟真实的浏览器。而不是这一行:

        request.AddHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0"
    

    我们需要添加 5 行:

        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        request.AddHeader "Accept-Language", "en-US,en;q=0.5"
        request.AddHeader "DNT", "1"
        request.AddHeader "Connection", "close"
    

    最后一个子:

    Sub getYahooFinanceData(stockTicker As String, StartDate As String, EndDate As String, frequency As String, cookie As String, crumb As String)
    ' forked from:
    ' http://investexcel.net/multiple-stock-quote-downloader-for-excel/
    
    Dim resultFromYahoo As String
    Dim objRequest
    Dim csv_rows() As String
    Dim tickerURL As String
    
    'Make URL
    tickerURL = "https://query1.finance.yahoo.com/v7/finance/download/" & stockTicker & _
        "?period1=" & StartDate & _
        "&period2=" & EndDate & _
        "&interval=" & frequency & "&events=history" & "&crumb=" & crumb
    '***************************************************
                 
    'Get data from Yahoo
    #If Mac Then
    
        Dim client As New WebClient
        Dim response As WebResponse
        Dim request As New WebRequest
        
        client.BaseUrl = tickerURL
        request.Method = HttpGet
        request.Format = PlainText
        request.AddCookie "Cookie", cookie
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        request.AddHeader "Accept-Language", "en-US,en;q=0.5"
        request.AddHeader "DNT", "1"
        request.AddHeader "Connection", "close"
    
        Set response = client.Execute(request)
        DoEvents
    
        If response.StatusCode = Ok Then
            resultFromYahoo = response.Content
        Else
            MsgBox "An error occured while getting data for '" & stockTicker & "'", vbInformation
            Exit Sub
        End If
        
        
    #Else
        Set objRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
        With objRequest
            .Open "GET", tickerURL, False
            .SetRequestHeader "Cookie", cookie
            .Send
            .WaitForResponse
            resultFromYahoo = .ResponseText
        End With
    #End If
    
    
    '***************************************************
    csv_rows() = Split(resultFromYahoo, Chr(10))
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多