簡體   English   中英

是否有一段代碼可以從 Google 搜索中提取地址

[英]Is there a piece of code which will pull an address out from a Google Search

因此,當您在 Google 上搜索 [公司名稱] +“地址”時,我試圖獲取公司的地址。 我可以得到“大約 20,500 個結果(0.49 秒)”沒有問題,因為 div id 始終是 resultStats,但是地址是 HTML Class,稱為:Z0LcW。 不確定它是否會改變。

我正在嘗試確認潛在供應商提供的交易地址是否有效。 它適用於 4k 地址,我不想手動驗證它。

這個想法來自: 使用VBA in Excel to Google Search a keyword and return indexed pages on google

我看過這些在概念上很有幫助的人: Using getElementsByClassName in Excel VBA

Dim XMLHTTP As Object, html, ObjResultdiv As Object
Dim start_time As Date
Dim end_time As Date

lastRow = Range("A" & Rows.Count).End(xlUp).Row

Dim cookie As String
Dim result_cookie As String

start_time = Time
Debug.Print "start_time:" & start_time

For i = 1 To lastRow

    url = "https://www.google.co.uk/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)

    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", url, False
    XMLHTTP.setRequestHeader "Content-Type", "text/xml"
    XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
    XMLHTTP.send

    Set html = CreateObject("htmlfile")
    html.body.innerHTML = XMLHTTP.responseText

    'Debug.Print XMLHTTP.ResponseText


If html.getElementsBytagname("Z0LcW") Is Nothing Then
    str_text = "0 Results"
Else
    str_text = html.getElementsBytagname("Z0LcW")
End If
    Cells(i, 2) = str_text
    DoEvents
Next

end_time = Time
Debug.Print "end_time:" & end_time

Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)

答案應該是:31B, Avenue One, Station Ln, Witney OX28 4XZ

https://www.google.co.uk/search?sxsrf=ACYBGNQ6p6lh1ZFfS6dGwsDcg45wKgmICw%3A1569864526381&ei=TjuSXaPnFvHC8gL81a2gCg&q=beadlight+ltd+address&oq=beadlight+ltd+address&gs_l=psy-ab.3...1272.2467..2622...0.0 ..0.85.635.8......0..1..gws-wiz.......0j0i22i30j33i160j33i21.NErA1FvTnr8&ved=0ahUKEwijxsKLifnkAhVxoVwKHfxqC6QQ4dUDCAs&uact=5

添加了對 Microsoft HTML Object 庫的引用,以便我們可以Dim HTMLDocument並使用那里可用的方法。

“Z0LcW”是一個 class,所以我使用 getElementsByClassName("Z0LcW") 而不是 getElementsByTagName 返回結果。

一旦我們真正得到結果,我們就可以使用元素的.innerText屬性來獲取文本值。 我不是類和標簽方面的專家,因此請注意在某些時候可能存在重復項,因此您稍后可能需要遍歷一組元素。

Dim XMLHTTP As Object, ObjResultdiv As Object
Dim start_time As Date
Dim end_time As Date
Dim html As New HTMLDocument

lastRow = Range("A" & Rows.Count).End(xlUp).Row

Dim cookie As String
Dim result_cookie As String

start_time = Time
Debug.Print "start_time:" & start_time

For i = 1 To lastRow

    Url = "https://www.google.co.uk/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)

    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", Url, False
    XMLHTTP.setRequestHeader "Content-Type", "text/xml"
    XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
    XMLHTTP.send

    'Set html = CreateObject("htmlfile") 'Don't need this as we early bind an HTMLDocument with the methods we need
    html.body.innerHTML = XMLHTTP.responseText

    'Debug.Print XMLHTTP.ResponseText


If html.getElementsByClassName("Z0LcW") Is Nothing Then
    str_text = "0 Results"
Else
    str_text = html.getElementsByClassName("Z0LcW")
End If

Debug.Print str_text.innerText 'innerText is the property that will give the text inside the elements found

    Cells(i, 2) = str_text.innerText
    DoEvents
Next

end_time = Time
Debug.Print "end_time:" & end_time

Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM