VBA宏可在搜索后从Google标头中检索信息

时间:2015-10-05 15:19:11

标签: excel vba excel-vba

我正在写一个宏,将进入谷歌搜索几个城市的县。到目前为止,我已经设法对宏搜索正确的术语,但我得到的唯一障碍就是得到结果。我不知道要写什么来检索它们。

当我进行搜索时,我得到了这个结果:

http://i.stack.imgur.com/O8jEh.png

我想要做的是找回“萨福克郡”线,但我对如何做到这一点感到迷茫。

是否有人有任何提示或知道如何做到这一点或寻找什么?我已经尝试检查该项目但无济于事。

这是我的代码到目前为止的样子:

Sub AutomateIE()
    Dim ie As InternetExplorer
    Dim City As String
    Dim State As String
    Dim URL As String

    Set ie = New InternetExplorer

    'For testing Purposes
    City = "boston"
    State = "MA"

     'Search google for state and county
     URL = "www.google.com/?safe=active&ssui=on#q=" + City + "+" + State + "+county&safe=active&ssui=on"
    ie.Navigate URL

     'Loop unitl ie page is fully loaded
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop

End Sub

1 个答案:

答案 0 :(得分:3)

您需要确保" Microsoft HTML对象库"和#34; Microsoft Internet Controls"可用。在VB窗口中,转到工具 - >引用,并在该库中放置一个复选标记(可能需要滚动才能找到它)。

试试这个:

Sub test()
Dim IE      As New InternetExplorer
Dim city$, state$

'For testing Purposes
city = "boston"
state = "MA"

 'Search google for state and county
URL = "www.google.com/?safe=active&ssui=on#q=" + city + "+" + state + "+county&safe=active&ssui=on"

IE.navigate URL
IE.Visible = False
Do
    DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading

Dim doc     As HTMLDocument  'variable for document or data which need to be extracted out of webpage
Set doc = IE.document
Dim dd      As Variant
dd = doc.getElementsByClassName("_eF")(0).innerText
' Now, trim the name to take the State out
dd = Left(dd, WorksheetFunction.Search(",", dd) - 1)
MsgBox dd
End Sub