代码之家  ›  专栏  ›  技术社区  ›  fp007

谷歌地图,抓取地址

  •  0
  • fp007  · 技术社区  · 7 年前

    我正在使用cheerio刮取下一个html。

    <div class="google-map-wrap " style="padding-bottom:50px;">
    <div class="google-map"></div><style>.google-map{width: 100%; height: 420px;} 
    .google-map img {max-width:none !important;}</style><script>jQuery(document).ready(function(){jQuery(".google-map").gmap3({marker:{address: "Periferico Sur 2020"},map:{options:{zoom: 14,}}});});
    

    我尝试了以下几种方法,但都没有奏效。

    预计=“Periferico Sur 2020”

    $('div.google-map-wrap div.google-map').text()
    
    $('div.google-map-wrap div.google-map').html()
    
    $('div.google-map-wrap div.google-map').css()
    

    但不起作用的结果应该是地址。

    1 回复  |  直到 7 年前
        1
  •  0
  •   fp007    7 年前

    如前所述,cheerio将无法做到这一点。但是request已经有了html,所以我们可以使用它来查找indexOf()的字符串。例子:

    .html

    <div class="google-map-wrap " style="padding-bottom:50px;">
    <div class="google-map"></div><style>.google-map{width: 100%; height: 420px;} 
    .google-map img {max-width:none !important;}</style>
    <script>jQuery(document).ready(function(){jQuery(".google-map").gmap3({marker:{address: "Periferico Sur 2020"},map:{options:{zoom: 14,}}});});
    

    .js公司

    var readAddress = function(html){
     var idx1 = html.indexOf('{marker:{address: "');
     var idx2 = idx1 + '{marker:{address: "'.length;
     var idx3 = html.indexOf('"', idx2);
    
    address = html.substring(idx2, idx3);
    }
    
    request(url, (err, res, data) => {
        readAddress(data);
     });