Return 使用 VBA 从 Google 项搜索到 excel 的地址
Return Address from Google term search to excel using VBA
我熟悉 Whosebug,但最近才注册。我正在尝试使用 VBA 在 google 和 return 中搜索 Excel 中的地址。下面是我试图从 Google return 得到的信息的照片。根据我的研究,我能够找到 VBA 允许我 return 结果统计。
是否可以修改我的代码和 return 我 google 搜索顶部的框?
非常感谢您的帮助!下面是我用来 return 搜索结果的 VBA。
Sample Image - Red Roof Inn & Address
Sub SearchGoogle()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
Dim var1 As Object
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
With ie
.Visible = True
.navigate "http://www.google.co.in"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("lst-ib").Value = var
'Here we are clicking on search Button
Set form = ie.document.getElementsByTagName("form")
Application.Wait (Now + TimeValue("0:00:02"))
Set button = form(0).onsubmit
form(0).submit
'wait for page to load
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
Set var1 = ie.document.getElementById("resultStats")
Cells(x, 2).Value = var1.innerText
ie.Quit
Set ie = Nothing
Next x
End Sub
现在您的代码加载页面,然后加载 resultStats
元素的值。
因此您需要更改的代码部分是:
Set var1 = ie.document.getElementById("resultStats")
Cells(x, 2).Value = var1.innerText
解决您的问题的第一步是了解您尝试使用的 HTML 页面的 DOM,在本例中为 Google。我建议使用浏览器来浏览 DOM,因为它可以让您很好地了解整个页面在做什么。
如果您的目标是在宏观基础上执行此操作,您将需要一条通过 DOM 的路径,它将始终带您到达您想去的地方。我建议打开两个包含不同搜索的页面,以便您可以随时检查您的假设。
例如,您提到的框似乎位于名为 kp-header
的 class 中,您可以通过了解这一点来构建从 DOM 到 [=38] 的路径=] 屏幕上显示的文本值。同样,您将需要自己进行调查以找到最适合您搜索的陈述点,因为 kp-header
只是我能找到的第一个非常有用的结果。
尽管请注意,根据您加载这些网页的速度,您可能会达到 google 的限制,因为它们不鼓励抓取。避免这些限制并避免自己必须调查所有 google 的 DOM 的更好选择是尝试合并 google 的 API的
我熟悉 Whosebug,但最近才注册。我正在尝试使用 VBA 在 google 和 return 中搜索 Excel 中的地址。下面是我试图从 Google return 得到的信息的照片。根据我的研究,我能够找到 VBA 允许我 return 结果统计。
是否可以修改我的代码和 return 我 google 搜索顶部的框?
非常感谢您的帮助!下面是我用来 return 搜索结果的 VBA。
Sample Image - Red Roof Inn & Address
Sub SearchGoogle()
Dim ie As Object
Dim form As Variant
Dim button As Variant
Dim LR As Integer
Dim var As String
Dim var1 As Object
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To LR
var = Cells(x, 1).Value
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
With ie
.Visible = True
.navigate "http://www.google.co.in"
While Not .readyState = READYSTATE_COMPLETE
Wend
End With
'Wait some to time for loading the page
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
ie.document.getElementById("lst-ib").Value = var
'Here we are clicking on search Button
Set form = ie.document.getElementsByTagName("form")
Application.Wait (Now + TimeValue("0:00:02"))
Set button = form(0).onsubmit
form(0).submit
'wait for page to load
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02"))
Set var1 = ie.document.getElementById("resultStats")
Cells(x, 2).Value = var1.innerText
ie.Quit
Set ie = Nothing
Next x
End Sub
现在您的代码加载页面,然后加载 resultStats
元素的值。
因此您需要更改的代码部分是:
Set var1 = ie.document.getElementById("resultStats")
Cells(x, 2).Value = var1.innerText
解决您的问题的第一步是了解您尝试使用的 HTML 页面的 DOM,在本例中为 Google。我建议使用浏览器来浏览 DOM,因为它可以让您很好地了解整个页面在做什么。
如果您的目标是在宏观基础上执行此操作,您将需要一条通过 DOM 的路径,它将始终带您到达您想去的地方。我建议打开两个包含不同搜索的页面,以便您可以随时检查您的假设。
例如,您提到的框似乎位于名为 kp-header
的 class 中,您可以通过了解这一点来构建从 DOM 到 [=38] 的路径=] 屏幕上显示的文本值。同样,您将需要自己进行调查以找到最适合您搜索的陈述点,因为 kp-header
只是我能找到的第一个非常有用的结果。
尽管请注意,根据您加载这些网页的速度,您可能会达到 google 的限制,因为它们不鼓励抓取。避免这些限制并避免自己必须调查所有 google 的 DOM 的更好选择是尝试合并 google 的 API的