Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-web刮取找不到正确的GET请求_Excel_Vba_Web Scraping_Xmlhttprequest - Fatal编程技术网

Excel VBA-web刮取找不到正确的GET请求

Excel VBA-web刮取找不到正确的GET请求,excel,vba,web-scraping,xmlhttprequest,Excel,Vba,Web Scraping,Xmlhttprequest,我的问题与另一个问题有关。我也有类似的问题 网站网址- 我需要获得货币参考日期和所选值。问题是我找不到正确的GET请求,这些值最终都是在那里生成的。我发现它与POST请求有关: POST/en/core functions/monetary policy/exchange list/exchange list?p_p_id=tecajnalistacontroller\u WAR\hnbtecajnalistaportlet&p_p_生命周期=2&p_p_生命周期=2&p_p_状态=normal

我的问题与另一个问题有关。我也有类似的问题

网站网址-

我需要获得货币参考日期和所选值。问题是我找不到正确的
GET
请求,这些值最终都是在那里生成的。我发现它与
POST
请求有关:

POST
/en/core functions/monetary policy/exchange list/exchange list?p_p_id=tecajnalistacontroller\u WAR\hnbtecajnalistaportlet&p_p_生命周期=2&p_p_生命周期=2&p_p_状态=normal&p_p_模式=view&p_p_p_资源id=gettecajnajaxdataurl&p_p_可缓存性=cacheLevelPage&p_p_col id=col计数=2 HTTP/1.1

我想使用一种通过id、类或标记获取信息的技术,但无论如何,只要
GET
URL请求太快,无法检索所需信息XMLHTTP请求和API: 我会使用如下所示的方法。我有一些帮助函数来帮助解析响应。在
GetDict
功能中,您可以设置感兴趣的货币。在函数
GetRate
中,您可以指定感兴趣的速率。如果不指定,则默认值为
“中间费率”

调用API:

要获取特定日期的费率,请对 以下网址:

日期参数是可选的。如果未设置,则为当前日期(今天) 使用

您可以使用一个字符串解析
JSON
响应,但我发现使用
Split
JSON
字符串中获取所需信息更简单。如果您熟悉
JSON
,我很乐意更新一个JSON解析示例

Option Explicit

Public Sub GetInfo()
    'http://hnbex.eu/api/v1/
    Dim strJSON As String, http As Object, json As Object
    Const URL As String = "http://hnbex.eu/api/v1/rates/daily/"

    Set http = CreateObject("MSXML2.XMLHTTP")
    With http
        .Open "GET", URL, False
        .send
        strJSON = .responseText
    End With
    'Set json = JsonConverter.ParseJson(strJSON) '<== You could parse the JSON using a JSON parse such as [JSONConverter][1]

    Dim currencyDict As Object
    Set currencyDict = GetDict

    Dim key As Variant, dictKeys As Variant, result As Variant
    For Each key In currencyDict.keys
        result = GetRate(strJSON, key)
        If Not IsError(result) Then currencyDict(key) = result
        result = vbNullString
    Next key

    PrintDictionary currencyDict

End Sub

Public Function GetDict() As Object '<== You could adapt to pass currencies as string arguments to the function. Or even a string array.
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    dict.Add "EUR", vbNullString
    dict.Add "CZK", vbNullString
    dict.Add "HRK", vbNullString
    dict.Add "HUF", vbNullString
    dict.Add "PLN", vbNullString
    dict.Add "RON", vbNullString
    dict.Add "RSD", vbNullString
    Set GetDict = dict
End Function

Public Function GetRate(ByVal json As String, ByVal key As Variant, Optional ByVal rate As String = "median_rate") As Variant
    Dim arr() As String, tempString As String
    On Error GoTo Errhand
    arr = Split(json, """currency_code"": " & Chr$(34) & key & Chr$(34))
    tempString = arr(1)
    tempString = Split(arr(1), Chr$(34) & rate & Chr$(34) & ":")(1)
    tempString = Split(tempString, ",")(0)
    GetRate = tempString
    Exit Function
Errhand:
    GetRate = CVErr(xlErrNA)
End Function

Public Sub PrintDictionary(ByVal dict As Object)
    Dim key As Variant
    For Each key In dict.keys
        Debug.Print key & " : " & dict(key)
    Next
End Sub
选项显式
公共子GetInfo()
'http://hnbex.eu/api/v1/
Dim strJSON作为字符串,http作为对象,json作为对象
常量URL作为字符串=”http://hnbex.eu/api/v1/rates/daily/"
设置http=CreateObject(“MSXML2.XMLHTTP”)
使用http
.打开“获取”,URL,False
.发送
strJSON=.responseText
以
'Set json=JsonConverter.ParseJson(strJSON)'XMLHTTP请求和API:
我会使用如下所示的方法。我有一些帮助函数来帮助解析响应。在
GetDict
功能中,您可以设置感兴趣的货币。在函数
GetRate
中,您可以指定感兴趣的速率。如果不指定,则默认值为
“中间费率”

调用API:

要获取特定日期的费率,请对 以下网址:

日期参数是可选的。如果未设置,则为当前日期(今天) 使用

您可以使用一个字符串解析
JSON
响应,但我发现使用
Split
JSON
字符串中获取所需信息更简单。如果您熟悉
JSON
,我很乐意更新一个JSON解析示例

Option Explicit

Public Sub GetInfo()
    'http://hnbex.eu/api/v1/
    Dim strJSON As String, http As Object, json As Object
    Const URL As String = "http://hnbex.eu/api/v1/rates/daily/"

    Set http = CreateObject("MSXML2.XMLHTTP")
    With http
        .Open "GET", URL, False
        .send
        strJSON = .responseText
    End With
    'Set json = JsonConverter.ParseJson(strJSON) '<== You could parse the JSON using a JSON parse such as [JSONConverter][1]

    Dim currencyDict As Object
    Set currencyDict = GetDict

    Dim key As Variant, dictKeys As Variant, result As Variant
    For Each key In currencyDict.keys
        result = GetRate(strJSON, key)
        If Not IsError(result) Then currencyDict(key) = result
        result = vbNullString
    Next key

    PrintDictionary currencyDict

End Sub

Public Function GetDict() As Object '<== You could adapt to pass currencies as string arguments to the function. Or even a string array.
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    dict.Add "EUR", vbNullString
    dict.Add "CZK", vbNullString
    dict.Add "HRK", vbNullString
    dict.Add "HUF", vbNullString
    dict.Add "PLN", vbNullString
    dict.Add "RON", vbNullString
    dict.Add "RSD", vbNullString
    Set GetDict = dict
End Function

Public Function GetRate(ByVal json As String, ByVal key As Variant, Optional ByVal rate As String = "median_rate") As Variant
    Dim arr() As String, tempString As String
    On Error GoTo Errhand
    arr = Split(json, """currency_code"": " & Chr$(34) & key & Chr$(34))
    tempString = arr(1)
    tempString = Split(arr(1), Chr$(34) & rate & Chr$(34) & ":")(1)
    tempString = Split(tempString, ",")(0)
    GetRate = tempString
    Exit Function
Errhand:
    GetRate = CVErr(xlErrNA)
End Function

Public Sub PrintDictionary(ByVal dict As Object)
    Dim key As Variant
    For Each key In dict.keys
        Debug.Print key & " : " & dict(key)
    Next
End Sub
选项显式
公共子GetInfo()
'http://hnbex.eu/api/v1/
Dim strJSON作为字符串,http作为对象,json作为对象
常量URL作为字符串=”http://hnbex.eu/api/v1/rates/daily/"
设置http=CreateObject(“MSXML2.XMLHTTP”)
使用http
.打开“获取”,URL,False
.发送
strJSON=.responseText
以

'Set json=JsonConverter.ParseJson(strJSON)'该网页的内容通过javascript加载。你应该遵循QHarr已经提出的建议,或者使用IE获得它们。该网页的内容通过javascript加载。你应该遵循QHarr已经提出的建议,或者使用IE获得它们。获得
剪贴板的粉丝。非常感谢。你帮了我很多!很高兴。很高兴它有帮助。得到了
剪贴板的粉丝
。非常感谢。你帮了我很多!很高兴。很高兴这有帮助。