简体   繁体   中英

Google Apps Script regex returning null (data scraping) (google sheets)

Goal: The goal is to grab the shiller PE from this website and to put it in a google sheet that updates every 24 hours.

Website: https://www.gurufocus.com/sector_shiller_pe.php

The table on the website with "shiller pe" values that needs to be scraped

Live values needed to be put in this google sheet

Solutions Tried:

  1. =IMPORTXML("URL","XPATH")

Tried to use this function in the spreadsheet, but it is just unable to grab the website because the website is too slow

  1. Google Apps Script

Made this script to grab the HTML of the data and return the shiller value for the Energy sector:

function energyFinder() {
  var url = "https://www.gurufocus.com/sector_shiller_pe.php";
  const html = UrlFetchApp.fetch(url).getContentText();
  var regex = new RegExp(/\/mq5xwwf">26<\/a><\/b><\/u><\/td><td class="text_center">([0-9][0-9].[0-9][0-9])<\/td>/);
  var e = regex.exec(html);
  return e
Logger.log(e)

}

But this just returns null. Any solution would help, thanks!

ps new to the website so I cannon embed pictures yet, so sorry for that

You probably have an issue with the regex itself as there's no quotation mark before 26 .

This should work:

\/mq5xwwf?>26<\/a><\/b><\/u><\/td><td class='text_center'>([0-9][0-9]\.[0-9][0-9])<\/td>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM