Search code examples
javascriptcheerio

Google Maps, scraping address


I'm using cheerio to scrape the next 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,}}});});

I tried the following options and they haven't worked.

Expected = "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()

But not working the result should be the address.


Solution

  • As commented before, cheerio won't be able to do it. But request already has the html so we can use it to find the string with indexOf(). Example:

    .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);
     });