【发布时间】:2018-12-20 04:40:01
【问题描述】:
我知道这看起来很容易。我已经输入了一个代码来尝试让它工作,但遇到了一个问题。以下链接上的格式对于所有城市和州都是相同的。只要您可以输入城市名称(“City_Search”)和州名(“State_Search”),您就应该能够使用如下所示的信息访问该网站。
我在下面附上了我正在使用的公式。如果有人可以帮助我进行搜索,我将不胜感激。
Sub SearchBot1()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim HTMLinputs As MSHTML.IHTMLElementCollection
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://datausa.io/profile/geo/" & Range("City_Search").Value & "-" & Range("State_Search").Value
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
End Sub
我的想法是让我在 Excel 中输入任何城市,一旦我在宏上运行,它将转到该站点并搜索城镇数据。我在下面添加了一个链接作为我在搜索时希望获得的页面的示例。
【问题讨论】:
标签: vba excel web web-scraping