【问题标题】:How to crawl Googlemaps from VBA Excel?如何从 VBA Excel 抓取 Googlemaps?
【发布时间】:2019-09-25 08:42:55
【问题描述】:

作为一名真正的编码初学者,我正在这里寻求帮助!

我想从 GoogleMaps 中提取数据:比如说,邮政编码,从公司输入的 excel 单元格中,通过 VBA。

当我开始编码时,我在字符串级别遇到了困难,因为我无法定位准确的标签(我认为,但这就是重点,跨度)并有效地提取 Googlemaps HTML 上地址行上的邮政编码页面:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row = Range("nom").Row And _
    Target.Column = Range("nom").Column Then
     Dim GM As New InternetExplorer
     GM.Visible = True
     GM.navigate "https://www.google.fr/maps/@43.3082377,5.4259519,17z?q=" & Range("nom").Value
     Do
     DoEvents
     Loop Until GM.readyState = READYSTATE_COMPLETE
     Dim doc As HTMLDocument
     Set doc = GM.document
     Dim sSPAN As String
     sSPAN = Trim(doc.getElementsByTagName("span")(4).innerText)
     MsgBox sSPAN

    End If
End Sub

第 13 行显示以下错误:

运行滴度错误 91:对象变量或未设置块变量

因为我无法选择准确的块。

接下来,我有两个问题:

  1. 是否可以有效地从 Google 地图中提取数据? (通过 VBA 或开源的 Google API,从 Google 表格中提取(10000 行 excel)
  2. 有人遇到过这种困难吗?如何在 Googlemaps HTML 页面上选择准确的区块?

【问题讨论】:

  • 在这里您将找到有关可用的Google Maps APIs 的概述。我建议使用比网络爬虫更可靠的 API。 API 一开始可能看起来更难,但以后每次 Google 更改其在地图上的输出时,都需要修复网络爬虫。

标签: excel vba google-maps web-crawler extract


【解决方案1】:

我不会这样做,API 绝对是更可取的,尽管不是通过 GoogleMaps,除非您正在构建一个应用程序,您还可以在其中嵌入所需的地图。

您需要适当的页面加载等待,等待元素出现,最后退出应用程序,并使用不同的选择器策略。您还想实现一些逻辑来从地址中提取邮政编码(我不确定您得到了什么视图。我使用了Enterome 的测试值):

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim t As Date, gm As New InternetExplorer
    Const MAX_WAIT_SEC As Long = 10

    If Target.Row = Range("nom").Row And Target.Column = Range("nom").Column Then

        gm.Visible = True
        gm.navigate "https://www.google.fr/maps/@43.3082377,5.4259519,17z?q=" & Range("nom").Value

        While gm.Busy Or gm.readyState <> 4: DoEvents: Wend

        Dim elem As Object

        t = Timer
        Do
            On Error Resume Next
            Set elem = gm.document.querySelector(".section-info-text")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While elem Is Nothing

        If Not elem Is Nothing Then
            MsgBox Trim$(elem.innerText)
            'Implement logic to extract post code
        End If
        gm.Quit
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    相关资源
    最近更新 更多