在VBA中通过XMLHTTP发送表单数据

发布于 2024-11-29 00:24:17 字数 1199 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涙—继续流 2024-12-06 00:24:17

这段代码对我有用:(

Function SearchPlayer(playerName As String) As String

Dim xml As MSXML2.XMLHTTP60
Dim result As String

Const BASE_URL As String = "http://search.espn.go.com/results?searchString={name}&page=null&fromForm=true"

Set xml = CreateObject("MSXML2.XMLHTTP.6.0")

With xml
  .Open "GET", Replace(BASE_URL, "{name}", playerName), False
  .send
End With

result = xml.responseText

SearchPlayer = result

End Function

假设您的系统上有 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:

Function SearchPlayer(playerName As String) As String

Dim xml As MSXML2.XMLHTTP60
Dim result As String

Const BASE_URL As String = "http://search.espn.go.com/results?searchString={name}&page=null&fromForm=true"

Set xml = CreateObject("MSXML2.XMLHTTP.6.0")

With xml
  .Open "GET", Replace(BASE_URL, "{name}", playerName), False
  .send
End With

result = xml.responseText

SearchPlayer = result

End Function

(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).

热风软妹 2024-12-06 00:24:17

如果我没有遗漏什么,那么点击 Search 按钮后它所在的 URL 看起来像这样:http://www.espn.com/mlb/players?search=Fister

在此处输入图像描述

这是 GET 请求它返回 HTML,然后可以使用 MSHTMLDocument 的标准搜索功能进行搜索,例如:

Sub SearchPlayer()
    Dim http As MSXML2.ServerXMLHTTP
    Dim html As MSHTML.HTMLDocument ' Add reference to Microsoft HTML Object Library
    Dim url As String
    Dim player As String

    player = "Fister"
    url = "http://www.espn.com/mlb/players?search=" & player

    Set http = New MSXML2.ServerXMLHTTP
    http.Open "GET", url, False
    http.send

    Set html = New HTMLDocument
    html.body.innerHTML = http.responseText

    Dim nextGamePlace As MSHTML.HTMLDivElement
    Set nextGamePlace = html.querySelector("div[class='game-details'] div[class='venue']")

    Debug.Print nextGamePlace.textContent ' prints Miller Park
End Sub

注意:要观看请求,只需在浏览器中按 F12 并选择网络流量等等

If I am not missing something, then the URL where it goes after click on Search button looks like this: http://www.espn.com/mlb/players?search=Fister

enter image description here

It is GET request and it returns HTML which can be then searched e.g. using standard searching functions of MSHTMLDocument, example:

Sub SearchPlayer()
    Dim http As MSXML2.ServerXMLHTTP
    Dim html As MSHTML.HTMLDocument ' Add reference to Microsoft HTML Object Library
    Dim url As String
    Dim player As String

    player = "Fister"
    url = "http://www.espn.com/mlb/players?search=" & player

    Set http = New MSXML2.ServerXMLHTTP
    http.Open "GET", url, False
    http.send

    Set html = New HTMLDocument
    html.body.innerHTML = http.responseText

    Dim nextGamePlace As MSHTML.HTMLDivElement
    Set nextGamePlace = html.querySelector("div[class='game-details'] div[class='venue']")

    Debug.Print nextGamePlace.textContent ' prints Miller Park
End Sub

Note: to watch the requests, just hit F12 in your browser and select Network traffic etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文