Excel VBA查询外部.aspx页面并检索数据

时间:2013-12-18 15:53:16

标签: asp.net excel vba

我一直在努力争取这一天。基本上,我想编写一个excel宏来遍历excel中的列表,查询网页并检索一些数据。理想情况下,我只想检索我需要的数据,以便将其放在相邻的单元格中,但此时我会做任何事情。

该页面是ASP.net,我没有经验;如果它是.php我可能会管理,但我甚至不确定如何通过javascript发布到.aspx。

我可以很好地循环访问我的数据,一旦我得到数据,我就可以把它写成excel,所以有两个部分我正在努力:

第1部分 - 查询网页

This is the page I want to query。我需要在Property Address中搜索并从结果中检索数据。我将用于示例的地址是400 W Church St。我认为提交像“... / ParcelSearch.aspx?name = ...& value = ...”这样的表格可能很简单,但没有骰子。

第2部分 - 抓取数据

在结果中,顶部有一个表DetailsSummary_Master,其中的字段集使用<legend>标记定义。我需要<legend>Municipality</legend>中的数据: enter image description here

我无法弄清楚要做什么,循环遍历<td> s?我想也许我可以GetElementByID或者可能是标签,但我似乎无法弄明白。

VBA

到目前为止,我使用了一些SO线程试图找出它。 FirstSecondThird,但我似乎无法正确地发布POST。我暂时把这个潜艇分开了。

这就是我的问题(从其他线程中被盗)关于我的问题:

Sub SubmitForm()

Dim objIE As Object
Dim xmlhttp As Object
Dim ieButton As Object
Dim strResponse As String
Dim strUrl As String
Dim strID As String
Dim strValue As String
Dim strSubmit As String

strID = "?name=ctl00_ctl00_ctl00_ctl00_ContentMain_ContentMain_ContentMain_ContentMain_TabContainer1_Searches_SubTabContainer1_QuickSearches_CompositAddressSearch1_AddressSearch1_ctl00_Address&value="
strValue = "400 W Church St"
strSubmit = strID & strValue


strUrl = "http://www.ocpafl.org/searches/ParcelSearch.aspx"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate "about:blank"
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

'~~> Indicates that page that will receive the request and the type of request being submitted
xmlhttp.Open "POST", "http://www.ocpafl.org/searches/ParcelSearch.aspx", False

'~~> Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

'~~> Send the data as name/value pairs
xmlhttp.Send "strSubmit"
strResponse = xmlhttp.responseText
objIE.navigate strUrl
objIE.Visible = True

Do While objIE.readystate <> 4
    DoEvents
Loop

objIE.document.Write strResponse

Set xmlhttp = Nothing

End Sub

我实际上并不需要通过IE运行它,我想将它全部隐藏起来。我在Excel 2007上运行这个,但我在家里有2010年。我们也有荒谬的IE8,所以越少越好。我可以循环或使用数组,但我似乎无法与查询接口。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

为了进行查询,考虑到ASPX页面在回发时期望的表单字段的复杂性,您可能会发现在进行此调用时更容易控制浏览器。它会很慢,但应该有效。

一个相当可靠的工具是Selenium,还有一些插件可以控制Selenium from Excel VBA

编辑:此Excel VBA代码段应读出“Municipality Orlando”。您需要参数化以下代码,并为最终版本添加错误条件的案例,以便通过任何街道地址查询以获得其市政当局。这应该让你开始。我使用Selenium IDE和Firefox根据记录用户操作生成VBA代码,然后提出了一个XPath查询来获取文本。

  Dim selenium As New SeleniumWrapper.WebDriver
  selenium.Start "firefox", "http://www.ocpafl.org/searches/ParcelSearch.aspx"
  selenium.setImplicitWait 5000

  selenium.setImplicitWait 5000
  selenium.Open "/searches/ParcelSearch.aspx"

  selenium.Click "id=popup_ok"
  selenium.Type "id=ctl00_ctl00_ctl00_ctl00_ContentMain_ContentMain_ContentMain_ContentMain_TabContainer1_Searches_SubTabContainer1_QuickSearches_CompositAddressSearch1_AddressSearch1_ctl00_Address", "400 W Church St"
  selenium.Click "id=ctl00_ctl00_ctl00_ctl00_ContentMain_ContentMain_ContentMain_ContentMain_TabContainer1_Searches_SubTabContainer1_QuickSearches_PropertyNameSearch1_ctl00"
  selenium.Click "id=ctl00_ctl00_ctl00_ctl00_ContentMain_ContentMain_ContentMain_ContentMain_TabContainer1_Searches_SubTabContainer1_QuickSearches_CompositAddressSearch1_AddressSearch1_ctl00_ActionButton1"
  Dim municipalityResult As String
  municipalityResult = selenium.getText("//fieldset[contains(legend,'Municipality')]")
  selenium.stop