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

后端 未结 6 1929
刺人心
刺人心 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 03:52

    I ran into this exact same problem. I was able to circumvent it by first using the deprecated Xml.parse, since it still works, then selecting the body XmlElement, then passing in its Xml String into the new XmlService.parse method:

    var page = UrlFetchApp.fetch(contestURL);
    var doc = Xml.parse(page, true);
    var bodyHtml = doc.html.body.toXmlString();
    doc = XmlService.parse(bodyHtml);
    var root = doc.getRootElement();
    

    Note: This solution may not work if the old Xml.parse is completely removed from Google Scripts.

提交回复
热议问题