How to parse an HTML string in Google Apps Script without using XmlService?
This has been discussed before - see this Q&A.
Unlike XML service, the XMLService
is not very forgiving of malformed HTML. The trick in the answer by Justin Bicknell does the job. Even though XML service has been deprecated, it still continues to work.
I have done this in vanilla js. Not real html parsing. Just try to get some content out of a string (url):
function getLKKBTC() {
var url = 'https://www.lykke.com/exchange';
var html = UrlFetchApp.fetch(url).getContentText();
var searchstring = '<td class="ask_BTCLKK">';
var index = html.search(searchstring);
if (index >= 0) {
var pos = index + searchstring.length
var rate = html.substring(pos, pos + 6);
rate = parseFloat(rate)
rate = 1/rate
return parseFloat(rate);
}
throw "Failed to fetch/parse data from " + url;
}
I made cheeriogs for your problem. it's works on GAS as cheerio which is jQuery-like api. You can do that like this.
const content = UrlFetchApp.fetch('https://example.co/').getContentText();
const $ = Cheerio.load(content);
Logger.log($('p .blah').first().text()); // blah blah blah ...
See also https://github.com/asciian/cheeriogs