在VBA中通过XMLHTTP发送表单数据
我正在尝试通过 XMLHTTP 对象发送表单数据以获取网页。
我使用的是 Excel 2010。
网站是 http://espn.go.com/mlb/players。
我正在尝试通过搜索框搜索某个玩家(例如 Fister)。
这是表单标签之间的源代码。
<form id="searchBox" name="searchBox" action="http://search.espn.go.com/results" method="get" accept-charset="utf-8" style="color: #999999;">
<div class="clearfix">
<input autocomplete="off" class="text" type="text" placeholder="Search" name="searchString" id="searchString" />
<input type="hidden" name="page" id="page" value="null" />
<input type="hidden" name="fromForm" value="true" />
<input class="submit" type="submit" value="" />
</div>
</form>
我的代码。
Sub SearchPlayer()
Dim xml As MSXML2.ServerXMLHTTP
Dim search, url As String
search = "searchString=Fister&page=null&fromForm=true"
url = "http://espn.go.com/mlb/players"
Set xml = New MSXML2.ServerXMLHTTP
xml.Open "POST", url, False
xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xml.send search
MsgBox xml.responseText
Set xml = Nothing
End Sub
I'm trying to send form data through the XMLHTTP object to get a webpage.
I am using Excel 2010.
The website is http://espn.go.com/mlb/players.
I'm trying to search for a certain player through the searchbox (e.g. Fister).
Here is the source code between the form tags.
<form id="searchBox" name="searchBox" action="http://search.espn.go.com/results" method="get" accept-charset="utf-8" style="color: #999999;">
<div class="clearfix">
<input autocomplete="off" class="text" type="text" placeholder="Search" name="searchString" id="searchString" />
<input type="hidden" name="page" id="page" value="null" />
<input type="hidden" name="fromForm" value="true" />
<input class="submit" type="submit" value="" />
</div>
</form>
My code.
Sub SearchPlayer()
Dim xml As MSXML2.ServerXMLHTTP
Dim search, url As String
search = "searchString=Fister&page=null&fromForm=true"
url = "http://espn.go.com/mlb/players"
Set xml = New MSXML2.ServerXMLHTTP
xml.Open "POST", url, False
xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xml.send search
MsgBox xml.responseText
Set xml = Nothing
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码对我有用:(
假设您的系统上有 MSXML 6.0——本地 system32 文件夹中的 msxml6.dll)
如上所述,该表单使用 GET 请求,因此您可以使用 ACTION 属性并将 INPUT 标记的值附加到像这样的单个字符串:
http://search.espn.go.com/results?searchString=Fister&page=null&fromForm=true
我对 Sub 进行了功能化,这样你就可以用不同的玩家名称来调用它来抓取每一页。当然,如果您希望使用包含空格的玩家名称来调用它,则需要一个 urlencode 函数 (这是一个)。
This code worked for me:
(assumes you have MSXML 6.0 on your system -- msxml6.dll in your local system32 folder)
As stated, the form uses a GET request so you would use the ACTION attribute and append the INPUT tags' values onto a single string like this:
http://search.espn.go.com/results?searchString=Fister&page=null&fromForm=true
I functionized the Sub so you can call it with different player names to scrape each page. Of course, you would need a urlencode function if you expect it to be called with player names that have spaces in them (here's one).
如果我没有遗漏什么,那么点击
Search
按钮后它所在的URL
看起来像这样:http://www.espn.com/mlb/players?search=Fister这是
GET
请求它返回HTML
,然后可以使用MSHTMLDocument
的标准搜索功能进行搜索,例如:注意:要观看请求,只需在浏览器中按 F12 并选择网络流量等等
If I am not missing something, then the
URL
where it goes after click onSearch
button looks like this: http://www.espn.com/mlb/players?search=FisterIt is
GET
request and it returnsHTML
which can be then searched e.g. using standard searching functions ofMSHTMLDocument
, example:Note: to watch the requests, just hit F12 in your browser and select Network traffic etc.