带有VBA的XMLHTTP上的404状态,但URL在浏览器中打开

时间:2017-11-05 11:06:07

标签: vba xmlhttprequest

我试图从网站获取一些信息,但我获得状态404,即使网址在任何浏览器窗口中正确打开。 这是我使用的代码:

Sub GetURL()

Dim xhr As MSXML2.XMLHTTP60
Dim doc As MSHTML.HTMLDocument

Set xhr = New MSXML2.XMLHTTP60

Url = "http://sklep.trefl.com/en/puzzle.html#price%5Bfrom%5D=0&price%5Bto%5D=9999999&p=1&limit=30"

With xhr
    .Open "GET", Url, False
    .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
    .send
    If .readyState = 4 And .Status = 200 Then
        Set doc = New MSHTML.HTMLDocument
        wtext = .responseText
    Else
        Debug.Print "error"
    End If
End With

End Sub

可能是什么问题? 我曾尝试使用和不使用https。 我也试过编码"#" charcter to"%23"。

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以尝试没有“#price%5Bfrom%5D=0&price%5Bto%5D=9999999&p=1&limit=30”吗? - 只需“http://sklep.trefl.com/en/puzzle.html”即可获得正确的状态代码。

Sub GetURL()

Dim xhr As MSXML2.XMLHTTP60
Dim doc As MSHTML.HTMLDocument
Dim Url As String

Set xhr = New MSXML2.XMLHTTP60

Url = "http://sklep.trefl.com/en/puzzle.html"

With xhr
    .Open "GET", Url, False
    .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
    .send
    If .readyState = 4 And .Status = 200 Then
        Set doc = New MSHTML.HTMLDocument
        wtext = .responseText
    Else
        Debug.Print "error"
    End If
End With

End Sub

无论如何,您可以获取此网址的内容(http://sklep.trefl.com/en/puzzle.html#price%5Bfrom%5D=0&price%5Bto%5D=9999999&p=1&limit=30)。检查一下:

...
    Else
        Debug.Print .responseText
    End If
...