0

I'm trying to use the function below (source) to avoid using importxml in Google Sheets:

function importRegex(url, regex_string) {
  var html, content = '';
  var response = UrlFetchApp.fetch(url);
  if (response) {
    html = response.getContentText();
    if (html.length && regex_string.length) {
      var regex = new RegExp( regex_string, "i" );
      content = html.match(regex)[1];
    }
  }
  Utilities.sleep(1000);
  return content;  
}

I want to extract stock prices and day change from Yahoo Finance, for example from: https://finance.yahoo.com/quote/TSLA/, but I can't get the regex_string right. I have tried several strings but none seens to work. Some one can help me please?

=importRegex("https://finance.yahoo.com/quote/TSLA/", "<span class=""Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"" data-reactid=""32"">(.*)</span>")

  • 1
    Welcome to Web Applications. I'm not sure if this question should be considered on topic, by one side because it doesn't include enough details in the question as in order to answer it the readers should analyse an external resource, by the other hand regular expressions are not good for parsing web pages in general. Ref. Using regular expressions to parse HTML: why not? – Rubén Jun 11 at 4:45
  • 1
    One way "to fix" your question that you change the wording to ask how to build TEXT value that will be passed as string to Google Apps Script but you should include how it should look on the Google Apps Script side. – Rubén Jun 11 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.