What is the best way to parse html in google apps script

后端 未结 6 1932
刺人心
刺人心 2020-11-30 03:08
var page = UrlFetchApp.fetch(contestURL);
var doc = XmlService.parse(page);

The above code gives a parse error when used, however if I replace the

6条回答
  •  隐瞒了意图╮
    2020-11-30 04:03

    For simple tasks such as grabbing one value from a webpage, you could use a regular expression. Regex is notoriously bad for parsing HTML as there's all sorts of weird cases it can get tripped up, but if you're confident about the HTML you're accessing this can sometimes be the simplest way.

    Here's an example that fetches the contents of the page's </code> tag:</p> <pre><code>var page = UrlFetchApp.fetch(contestURL); var regExp = new RegExp("<title>(.*)", "gi"); var result = regExp.exec(page.getContentText()); // [1] is the match group when using parenthesis in the pattern var value = result ? result[1] : 'No title found';

提交回复
热议问题