[英]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.