使用 Excel VBA 代码发送 POST 并检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45283903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Excel VBA code to send a POST and retrieve data
提问by Darius M
What VBA code would allow me to send a POST request (in a asp search box) and then to retrieve data between a <span id="xxx"></span>
tag? (in the resulting page)
什么 VBA 代码允许我发送 POST 请求(在 asp 搜索框中)然后在<span id="xxx"></span>
标签之间检索数据?(在结果页面中)
I have the following code that simulates a search request in the page:
我有以下代码模拟页面中的搜索请求:
Dim Site As Object
Set Site = CreateObject("InternetExplorer.application")
Dim QNUMBER As String
Dim URL As String
URL = "apps/inventory/Default.aspx" 'local website
QNUMBER = textBox_Scan.Text
Site.navigate URL
While Site.busy
Wend
Dim oHTMLDoc As Object
Set oHTMLDoc = Site.document
oHTMLDoc.getElementById("input_search").Value = QNUMBER
oHTMLDoc.getElementById("btn_search").Click
It doesnt feel "clean" to do it that way and I feel that sending a POST request would be more appropriate.
这样做感觉并不“干净”,我觉得发送 POST 请求会更合适。
Thanks.
谢谢。
[edit]
[编辑]
This is the form code
这是表格代码
<form name="aspnetForm" method="post" action="Default.aspx" id="aspnetForm">
The input text id
输入文本 ID
id="input_search"
The submit button code
提交按钮代码
id="btn_search"
and I'd like to get data from <span id="warranty">36 month</span>
and <span id="budget">500$</span>
我想从<span id="warranty">36 month</span>
和获取数据<span id="budget">500$</span>
回答by Tiefan Ju
Sub macroPOST()
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "[Your URL]"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.Send ("id=dddaaa&pwd=1234[Your request parameters]")
replyTXT = objHTTP.responseText
If objHTTP.Status = "200" Then 'success
MsgBox replyTXT
Else
'Do something
End If
End Sub
It works on my Excel, thank you for reading carefully.
它适用于我的 Excel,感谢您仔细阅读。