2011-08-08 13 views
0

Excel 2010を使用しています。XMLHTTPオブジェクトを使用してフォームデータを送信しようとしています。私が働いているウェブサイトはhttp://espn.go.com/mlb/playersです。検索ボックス(Fisterなど)で特定のプレイヤーを検索しようとしています。ここにフォームタグ間のソースコードがあります。VBAでXMLHTTPを使用してフォームデータを送信

<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 
+2

HtmlフォームはGETメソッドを使用し、コードサンプルはPOSTを使用します。 – Filburt

答えて

0

このコードは、私の仕事:

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タグの値を次のように1つの文字列に追加します。

http://search.espn.go.com/results?searchString=Fister&page=null&fromForm=true

私はSubを機能させたので、各プレイヤーの名前で呼び出すことで各ページをスクラップできます。もちろん、スペースがあるプレーヤー名(here's one)で呼び出されると思われる場合は、urlencode関数が必要です。

関連する問題