从嵌入式谷歌地图中提取标记坐标

时间:2017-05-28 18:54:49

标签: json vba excel-vba google-maps web-scraping

这对我来说很新鲜。我需要从嵌入的谷歌地图中提取标记坐标 - 示例链接是http://www.picknpay.co.za/store-search,我想提取搜索中地图中生成的所有标记位置。考虑使用像ParseHub这样的服务,但在走这条路线之前,我以为我会用SO /我自己做一个镜头。

必须有一种更简单的方法来查找存储在地图中的标记的坐标,而不是手动浏览它们并单独搜索它们的坐标?

1 个答案:

答案 0 :(得分:5)

通过提供的链接http://www.picknpay.co.za/store-search提供的网页源HTML不包含必要的数据,它使用AJAX。网站http://www.picknpay.co.za提供了sorta API。响应以JSON格式返回。浏览页面e。 G。在Chrome中,然后打开“开发人员工具”窗口( F12 ),“网络”选项卡,重新加载( F5 )页面并检查记录的XHR。大多数相关数据是URL返回的JSON字符串:

http://www.picknpay.co.za/picknpay/json/picknpay/en/modules/store_finder/findStores.json

XHR-preview

XHR-headers

您可以使用以下VBA代码检索上述信息。 JSON.bas模块导入VBA项目以进行JSON处理。

Option Explicit

Sub Scrape_picknpay_co_za()

    Dim sResponse As String
    Dim sState As String
    Dim vJSON As Variant
    Dim aRows() As Variant
    Dim aHeader() As Variant

    ' Retrieve JSON data
    XmlHttpRequest "POST", "http://www.picknpay.co.za/picknpay/json/picknpay/en/modules/store_finder/findStores.json", "", "", "", sResponse
    ' Parse JSON response
    JSON.Parse sResponse, vJSON, sState
    If sState <> "Array" Then
        MsgBox "Invalid JSON response"
        Exit Sub
    End If
    ' Convert result to arrays for output
    JSON.ToArray vJSON, aRows, aHeader
    ' Output
    With ThisWorkbook.Sheets(1)
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aRows
        .Columns.AutoFit
    End With

    MsgBox "Completed"

End Sub

Sub XmlHttpRequest(sMethod As String, sUrl As String, arrSetHeaders, sFormData, sRespHeaders As String, sContent As String)

    Dim arrHeader

    'With CreateObject("Msxml2.ServerXMLHTTP")
    '    .SetOption 2, 13056 ' SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
    With CreateObject("MSXML2.XMLHTTP")
        .Open sMethod, sUrl, False
        If IsArray(arrSetHeaders) Then
            For Each arrHeader In arrSetHeaders
                .SetRequestHeader arrHeader(0), arrHeader(1)
            Next
        End If
        .send sFormData
        sRespHeaders = .GetAllResponseHeaders
        sContent = .responseText
    End With

End Sub

Sub OutputArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize(1, UBound(aCells) - LBound(aCells) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

Sub Output2DArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize( _
                UBound(aCells, 1) - LBound(aCells, 1) + 1, _
                UBound(aCells, 2) - LBound(aCells, 2) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

我的输出如下:

output

BTW,同样的方法适用于以下答案:123456789