After doing some tests with the site you provided and trying different methods I think it will be easier for you to simply retrieve the HTML static site text and then do a Javascript search on the site to return the values you are looking for. The code from the answer I shared is failing because the website has some HTML compatibillity issues as you are trying to interpret that data with XML instead. Moreover, the table you are interested in is within a div
element so you would need to first get to the div
element and then to the table.
The following piece of code with self-explanatory comments returns and logs the Forward P/E
values from the website you provided:
function ALTERNATIVE(){
// Get all the static HTML text of the website
const res = UrlFetchApp.fetch('https://finviz.com/quote.ashx?t=AAPL', {muteHttpExceptions: true}).getContentText();
// Find the index of the string of the parameter we are searching for
index = res.search("Forward P/E");
// create a substring to only get the right number values ignoring all the HTML tags and classes
sub = res.substring(index+68,index+73);
Logger.log(sub);
return sub;
}
References
- UrlFetchApp.fetch() to get the HTTP response data.
- getContentText() to return the HTTP response enconded as a String.
- Javascript search which returns the index index of the first match between the regular expression and the given string, or -1 if no match was found.
- Javascript substring which returns a new string containing the specified part of the given string starting from the first index and finishing with the second index.