【问题标题】:Empty response of an HTTP post request in VBAVBA 中 HTTP 发布请求的空响应
【发布时间】:2019-08-17 04:07:51
【问题描述】:

我正在尝试在 VBA 中发出 HTTP 发布请求,但得到一个空响应。 这是我的代码:

Sub User()
    On Error Resume Next
    Dim HTTPreq As WinHttpRequest
    Set HTTPreq = New WinHttpRequest
    URL = "https://www.transfermarkt.com/site/DropDownWettbewerbe"
    HTTPreq.Open "POST", URL, False
    HTTPreq.setRequestHeader "user-agent", "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/7.0.185.1002 Safari/537.36"
    HTTPreq.send "land_id=189"
    MsgBox (HTTPreq.responseText)
End Sub

我在 Python 中做了同样的事情并得到了预期的响应:

<pre><option value="">Competition</option><option value="GB1">Premier League</option><option value="GB2">Championship</option><option value="GB3">League One</option><option value="GB4">League Two</option><option value="CNAT">National League</option><option value="GB21">Premier League 2</option><option value="GB18">U18 Premier League</option><option value="GBFL">EFL Trophy</option><option value="FAC">FA Cup</option><option value="CGB">EFL Cup</option><option value="GBCS">Community Shield</option><option value="FAYC">FA Youth Cup</option>.

不知道我在 VBA 中做错在哪里。

【问题讨论】:

    标签: vba post web-scraping xmlhttprequest winhttprequest


    【解决方案1】:

    您需要指定content-type 标头,以下代码对我来说很好:

    Sub User()
    
        With CreateObject("WinHttp.WinHttpRequest.5.1")
            .Open "POST", "https://www.transfermarkt.com/site/DropDownWettbewerbe", False
            .SetRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
            .SetRequestHeader "user-agent", "user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64)"
            .Send "land_id=3"
            MsgBox .ResponseText
        End With
    
    End Sub
    

    您可以从浏览器开发者工具网络选项卡中找到必要的标头:

    【讨论】:

      猜你喜欢
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      相关资源
      最近更新 更多