Search code examples
google-apps-scriptgoogle-sheetsweb-scrapingcheerio

Get specific data from site using Google apps script


I am trying to get 4 data points from website, I am able to get first, but due to my novice skills I am having difficulty in getting other three. Here is the screenshot of webpage:

ScrapeData

I was able to get No. 1037325 from the following script:

function extractDataFromUrl() {

  var url = "https://h3p1c2bj.r.us-east-1.awstrack.me/L0/https:%2F%2Fduproprio.com%2Fen%2F1037325%3Futm_source=alerts%26utm_medium=email%26utm_campaign=qc-en-property%26utm_content=photo-propriete-2/1/0100018712213f2c-181f0f4a-951f-46e5-bc01-c2dc66276552-000000/ObF-c1cmnxhP6DpL7H7kY5QaxH0=314";

  // Fetch the contents of the redirected URL
  var response = UrlFetchApp.fetch(url);
  const regex = /#(\d+)/;
  const match = regex.exec(response);
  const result = match ? match[1] : null;

   Logger.log(result)  //1037325
 
}

Can you please guide how to get other data points? Any help would be greatly appreciated


Solution

  • try this:

    function extractPhoneNumbers(url) {
     // Fetch the page content
     var response = UrlFetchApp.fetch(url);
     var htmlContent = response.getContentText();
    
     // Create a regular expression to match the phone number pattern
     var regex = /<span class="listing-contact__number">\s*([\d-]+)\s*<\/span>/g;
    
     // Execute the regular expression on the HTML content
     var match;
     var phoneNumbers = [];
    
     while ((match = regex.exec(htmlContent)) !== null) {
      if (match[1]) {
       phoneNumbers.push(match[1]);
      }
     }
    
     // Log the extracted phone numbers
     if (phoneNumbers.length > 0) {
      Logger.log("Phone numbers: " + phoneNumbers.join(', '));
     } else {
      Logger.log("No phone numbers found.");
     }
    }
    
    function testExtractPhoneNumbers() {
     var url = 'https://duproprio.com/en/montreal/verdun/condo-for-sale/hab-395-rue-melrose-1022529?position=45.45362843523109%2C-73.57327222824098&zoom=15';
     extractPhoneNumbers(url);
    }