Parse values from HTML element using Google App Script?

微笑、不失礼 提交于 2021-02-08 06:14:56

问题


I am trying to parse HTML element by class on Google Sites, my code is:

function doGet(){

  var html = UrlFetchApp.fetch ('http://indicadoresdeldia.cl/').getContentText();
  var doc = XmlService.parse(html);
  var html = doc.getRootElement();
  var menu = getElementsByClassName(html, 'span3 utm')[0];
  var output = XmlService.getRawFormat().format(menu);
  return HtmlService.createHtmlOutput(output);

}

Ween i run the code appear the nexte error message ReferenceError: "getElementsByClassName" is not defined.

i am trying to deploy the example for the next page: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html

Any ideas?

THanks in advance for your help.


回答1:


According to that site, you should directly copy those functions to your project (source code available there) and then call them. That would alleviate each and every one of your problems.




回答2:


Source: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html

function getElementsByClassName(element, classToFind) {  
  var data = [];
  var descendants = element.getDescendants();
  descendants.push(element);  
  for(i in descendants) {
    var elt = descendants[i].asElement();
    if(elt != null) {
      var classes = elt.getAttribute('class');
      if(classes != null) {
        classes = classes.getValue();
        if(classes == classToFind) data.push(elt);
        else {
          classes = classes.split(' ');
          for(j in classes) {
            if(classes[j] == classToFind) {
              data.push(elt);
              break;
            }
          }
        }
      }
    }
  }
  return data;
}


来源:https://stackoverflow.com/questions/21218845/parse-values-from-html-element-using-google-app-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!