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

我一直在为这一天挣扎。 基本上,我想编写一个Excelmacros来循环在Excel中的列表,查询网页和检索一些数据。 理想情况下,我只是想检索我需要的数据,所以我可以把它放在一个相邻的单元格中,但是现在我可以做任何事情。

页面是ASP.net,我没有经验; 如果它是.php我可能可以pipe理,但我甚至不知道如何通过JavaScript张贴到.aspx。

我可以循环访问数据,一旦获得数据,我就可以将其写入excel,所以有两个部分我正在努力:

第1部分 – 查询网页

这是我想查询的页面 。 我需要在“ Property Addresssearch并从结果中检索数据。 我用这个例子的地址是400 W Church St 我认为可能很简单,提交一个像“… / ParcelSearch.aspx?name = …&value = …”,但没有骰子。

第2部分 – 抓取数据

在结果上,有一个表DetailsSummary_Master up top,带有由<legend>标签定义的字段集。 我需要<legend>Municipality</legend>在这里输入图像描述

我不知道该怎么做,通过<td> s循环? 我想也许我可以GetElementByID或可能通过标记,但我似乎无法弄清楚。

VBA

我用了几个SO线程来试图找出迄今为止。 第一 , 第二和第三 ,但我似乎甚至不能正确地开机自检。 我现在保持分开。

这是我的问题(从另一个线程偷来的):

 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,所以越less越好。 我可以循环或使用数组,但我似乎无法与查询接口。 任何帮助将不胜感激。

为了进行查询,考虑到ASPX页面在回发期间所期望的表单域的复杂性,您可能会发现在进行此调用时更容易控制浏览器。 这将是相当缓慢,但它应该工作。

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

编辑:这个Excel VBA代码片段应该读出“市奥兰多”。 你需要参数化下面的代码,并添加案件的错误条件为您的最终版本,以查询任何街道地址,以获得其市。 这应该让你开始,但。 我使用Firefox的Selenium IDE来生成基于logging用户操作的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