使用 Google apps 脚本从站点获取特定数据

问题描述 投票:0回答:1

我正在尝试从网站上获取 4 个数据点,我能够获得第一个,但由于我的新手技能,我很难获得其他三个。这是网页截图:

我能够从以下脚本中获得

No. 1037325

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
 
}

能否请您指导如何获取其他数据点?任何帮助将不胜感激

google-apps-script google-sheets web-scraping cheerio
1个回答
1
投票

试试这个:

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.