VBA:从新页面中提取HTML(相同的网址)

时间:2015-06-09 13:26:32

标签: html vba web-scraping

我需要向此网页http://kepler.sos.ca.gov/提供输入,然后在点击提交按钮后收集信息。第一部分(输入+点击提交)顺利运行:

Dim ie As InternetExplorer
'to refer to the HTML document returned
Dim html As HTMLDocument
'open Internet Explorer in memory, and go to website
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "http://kepler.sos.ca.gov/"
'Wait until IE is done loading page
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.document
html.getElementById("ctl00_content_placeholder_body_BusinessSearch1_RadioButtonList_SearchType_1").Click
html.getElementById("ctl00_content_placeholder_body_BusinessSearch1_TextBox_NameSearch").innerText = "global telematic solutions, LLC"
html.getElementById("ctl00_content_placeholder_body_BusinessSearch1_Button_Search").Click
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop

然后我需要在新页面的HTML脚本中获取元素的innertext。问题是我无法从新页面的源代码中提取信息。特别是我希望能够获得像

这样的东西
Dim SearchCount As String
SearchCount = html.getElementById("ctl00_content_placeholder_body_SearchResults1_TextInfoCorp1_TextInfoSearchResultCounts1_Label_RowCount").innerText

问题是“html”仍然指的是上一页。我如何参考新的?新页面的网址与上一页的网址相同。

1 个答案:

答案 0 :(得分:0)

最后一次单击后,您还需要另一个Set html = ie.document,以便拥有新的文档内容。

html.getElementById("ctl00_content_placeholder_body_BusinessSearch1_Button_Search").Click 
While ie.Busy Or ie.READYSTATE < 4: DoEvents: Wend
Set html = ie.document  '<== New HTML document content
相关问题