简体   繁体   English

使用Google App脚本解析HTML元素中的值?

[英]Parse values from HTML element using Google App Script?

I am trying to parse HTML element by class on Google Sites, my code is: 我正在尝试按类在Google协作平台上解析HTML元素,我的代码是:

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. 我在运行代码时出现了nexte错误消息ReferenceError:未定义“ getElementsByClassName”。

i am trying to deploy the example for the next page: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html 我正在尝试为下一页部署示例: https : //sites.google.com/site/scriptsexamples/learn-by-example/parsing-html

Any ideas? 有任何想法吗?

THanks in advance for your help. 在此先感谢您的帮助。

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. 这将减轻您的每一个问题。

Source: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html 来源: 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google Apps脚本解析XML元素中的所有值? - Parse all values from a XML element using Google Apps Script? 使用谷歌表格脚本从 HTML 中提取元素值 - Extracting element values from HTML using google sheet script Google 表格 + 应用脚本 + HTML 停止元素在 Google 表格侧边栏中的 onclick function 之后清除 - Google sheet + App Script + HTML Stop Element from clearing after onclick function in google sheet sidebar 我如何解析 <HTML> 作为Google App脚本中的字符串? - How do I parse a <HTML> as a string in Google App Script? 用户如何从html表单中收集Google应用程序脚本中变量输入值的值? - userHow to collect the values from html form for variable input values in google app script? 从 Google App Script 中的 HTML 中提取 URL - Extract URL from HTML in Google App Script 如何在不使用 XmlService 的情况下解析 Google Apps 脚本中的 HTML 字符串? - How to parse an HTML string in Google Apps Script without using XmlService? 使用 Google Apps Script 和 JavaScript 从 Google 表格中反映 HTML 中的选项值 - Reflect the Option Values in HTML from google sheet using Google Apps Script and JavaScript Google 脚本 - 从网站论坛解析 HTML - 并将数据写入工作表 - Google script - parse HTML from Website Forum - and Write Data to Sheet 从HTML页面运行Google表格应用脚本功能并返回表单值 - Running a Google Sheets App script function from an HTML page with form values returned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM