使用Excel中的VBA-Web刮取股票价格

时间:2015-12-30 12:38:28

标签: json excel excel-vba yql vba

我发现VBA-Web library对我正在为我的公司开发的项目看起来非常有趣。 基本上,我只需要从雅虎财经中剔除股票报价。 例如,对于IBM,我可以使用URL获取JSON:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20%22IBM%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=

我尝试了第一个简单的例子,但我遇到了错误。

这是代码:

Function GetQuote(Query As String) As String
    ' Create a WebClient for executing requests
    ' and set a base url that all requests will be appended to
    Dim QuotesClient As New WebClient
    QuotesClient.BaseUrl = "https://query.yahooapis.com/v1/public/yql?q="

    ' Use GetJSON helper to execute simple request and work with response
    Dim Resource As String
    Dim Response As WebResponse

    Resource = "select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20%22" & _
        Query & _
        "%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="
    Set Response = QuotesClient.GetJson(Resource)

    ProcessQuotes Response
End Function

Public Sub ProcessQuotes(Response As WebResponse)

    If Response.StatusCode = WebStatusCode.Ok Then
        Dim Route As Dictionary
        Set Route = Response.Data("query")(1)("results")(1)("quote")(1)

        Debug.Print "O preço é " & Route("Ask")("text")
    Else
        Debug.Print "Error: " & Response.Content
    End If
End Sub

这给了我:

Error: {"error":{"lang":"en-US","diagnostics":null,"description":"Query syntax error(s) [line 1:0 expecting statement_ql got '/']"}}

但是我在这段代码中做错了什么,非常感谢你的帮助!

0 个答案:

没有答案