excel - GET请求数据不更新

标签 excel vba api web-scraping

我正在尝试从 API 检索数据,但即使我在 GET 请求之前将其设置为空,我的变量也不会更新。

仅当我关闭 Excel 并重新打开它时,变量的数据才会更新。

有什么解释吗?我已经挠头好久了。

这是代码

Sub getJsonResult()
    Dim objRequestt As Object
    Dim strUrl As String
    Dim blnAsync As Boolean
    Dim strUrlXBTUSD As String
    Dim strResponse As String
    Dim jsonText As String
    Dim jsonObject As Object, item As Object
    Dim i As Integer

    'setting up the variable to 0 or nothing

    strUrlXBTUSD = ""
    strResponsee = ""
    jsonText = ""
    i = 0
 blnAsync = False
 Set item = Nothing
 Set jsonObject = Nothing
 Set objRequestt = Nothing
 Set objRequestt = CreateObject("MSXML2.XMLHTTP")
    strUrlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"
    blnAsync = True


    'Starting the GET request

    ThisWorkbook.Activate
    With objRequestt
        .Open "GET", strUrlXBTUSD, blnAsync
        .SetRequestHeader "Content-Type", "application/json"
        .send
        strResponse = .responseText 'here the response is always the same except if i Close Excel
           Debug.Print strResponsee
    End With
End Sub

最后,即使多次 F5 刷新,“strResponse”也始终相同。我可以看到网络浏览器上的数据不再准确。我希望 VBA 程序能够在不关闭 Excel 的情况下获取准确的数据并刷新。

如何做到这一点?

最佳答案

您可以添加一条指令以避免提供缓存结果(服务器可以忽略这一点,但我过去在这方面取得了很好的成功)。确保您的异步参数始终为 False,并在测试之间留出更多时间。我注意到有时价格变化很慢,因此您可能会由于间隔太小/尝试次数不足而错过变化。不过,您会注意到大小发生了变化。您应该添加 max timeout到底部脚本中的循环。

还删除了匈牙利符号。

Option Explicit

Public Sub getJsonResult()
    Dim http As Object
    Dim urlXBTUSD As String
    Dim response As String
    Dim j As Long
    Const ASYNC_ARG As Boolean = False

    Set http  = CreateObject("MSXML2.XMLHTTP")
    For j = 1 To 10
        response = vbNullString
        urlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"

        With http 
            .Open "GET", urlXBTUSD,  ASYNC_ARG
            .setRequestHeader "Content-Type", "application/json"
            .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
            .send
            response = .responseText
            Debug.Print response
        End With
        Application.Wait Now + TimeSerial(0, 0, 15)
    Next
End Sub

这是一种漫长而乏味的证明方法,通过循环直到返回集合中第一个项目的价格发生变化。我用jsonconverter.bas添加到项目和 VBE > 工具 > 引用 > Microsoft 脚本运行时引用。

Option Explicit

Public Sub getJsonResult()
    Dim http  As Object
    Dim urlXBTUSD As String
    Dim response As String
    Dim j As Long
    Const ASYNC_ARG As Boolean = False
    Dim price As String, firstValue As String

    Set http  = CreateObject("MSXML2.XMLHTTP")
    urlXBTUSD = "https://www.bitmex.com/api/v1/orderBook/L2?symbol=XBTUSD&depth=3"

    With http 
        .Open "GET", urlXBTUSD,  ASYNC_ARG
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .send
         firstValue = JsonConverter.ParseJson(.responseText)(1)("price")
        Debug.Print  firstValue
        Do
            .Open "GET", urlXBTUSD, blnAsync
            .setRequestHeader "Content-Type", "application/json"
            .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
            .send
            price = JsonConverter.ParseJson(.responseText)(1)("price")
            Application.Wait Now + TimeSerial(0, 0, 5)
        Loop While price = firstValue
        Debug.Print price
    End With
End Sub

关于excel - GET请求数据不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55200823/

相关文章:

database - 查询 Excel 表格

excel - 使用 CONCAT 执行部分搜索,不起作用

performance - Excel vba Application.screenupdating 与 Application.visible

vba - 范围并集在 VBA 函数中不起作用

api - 通过 API 的 Google 结帐发票

vba - 如何将csv字符串转换为列并在Excel中按特定关键字过滤?

vba - 我在 Excel 中制作语言翻译 VBA 案例时遇到问题

arrays - 隐藏重复的单元格而不使用辅助列

JavaScript 仅检测陆地或水域 Google map

c++ - Xerces-C:从 v2.x 迁移到 v3.x?