dom – Google Apps脚本有类似getElementById的内容吗?

dom – Google Apps脚本有类似getElementById的内容吗?,第1张

概述我将使用Google App Script从广播电台的网站上获取节目列表. 如何通过指定元素的id来选择网页中的指定元素? 因此,我可以在网页上获取程序. 编辑,2013年12月:Google已弃用旧的Xml服务,将其替换为 XmlService.此答案中的脚本已更新为使用新服务.新服务需要符合标准的XML& HTML,虽然旧的原谅了丢失关闭标签等问题. 查看Tutorial: Parsing a 我将使用Google App Script从广播电台的网站上获取节目列表.
如何通过指定元素的ID来选择网页中的指定元素?
因此,我可以在网页上获取程序.解决方法 编辑,2013年12月:Google已弃用旧的Xml服务,将其替换为 XmlService.此答案中的脚本已更新为使用新服务.新服务需要符合标准的XML& HTML,虽然旧的原谅了丢失关闭标签等问题.

查看Tutorial: Parsing an XML Document.(截至2013年12月,本教程仍然在线,虽然不推荐使用Xml服务.)从该基础开始,您可以利用脚本服务中的XML解析来导航页面.这是一个 *** 作你的例子的小脚本:

function getProgrammeList() {  txt = '<HTML> <body> <div> <div> <div ID="here">Hello World!!</div> </div> </div> </HTML>'  // Put the receIEved xml response into XMLdocument format  var doc = Xml.parse(txt,true);  Logger.log(doc.HTML.body.div.div.div.ID +" = "            +doc.HTML.body.div.div.div.Text );    /// here = Hello World!!  deBUGger;  // Pause in deBUGger - examine content of doc}

要获得真实页面,请从以下开始:

var url = 'http://blah.blah/whatever?querystring=foobar';var txt = UrlFetchApp.fetch(url).getContentText();....

如果查看getElements的文档,您将看到支持检索特定标记,例如“div”.找到特定元素的直接子元素,它不会探索整个XML文档.您应该能够编写一个遍历文档的函数来检查每个div元素的ID,直到找到您的程序列表.

var programmeList = finddivByID(doc,"here");

编辑 – 我忍不住了……

这是一个实用功能,可以做到这一点.

/** * Find a <div> tag with the given ID. * <pre> * Example: getdivByID( HTML,'tagVal' ) will find *  *          <div ID="tagVal"> * </pre> * * @param {Element|document} *                     element     XML document or element to start search at. * @param {String}     ID      HTML <div> ID to find. * * @return {XmlElement}        First matching element (in doc order) or null. */function getdivByID( element,ID ) {  // Call utility function to do the work.  return getElementByVal( element,'div','ID',ID );}/** * !Now updated for XmlService! * * Traverse the given Xml document or Element looking for a match. * Note: 'class' is stripped during parsing and cannot be used for * searching,I don't kNow why. * <pre> * Example: getElementByVal( body,'input','value','Go' ); will find *  *          <input type="submit" name="btn" value="Go" ID="btn"  /> * </pre> * * @param {Element|document} *                     element     XML document or element to start search at. * @param {String}     elementType XML element type,e.g. 'div' for <div> * @param {String}     attr        Attribute or Property to compare. * @param {String}     val         Search value to locate * * @return {Element}               First matching element (in doc order) or null. */function getElementByVal( element,elementType,attr,val ) {  // Get all descendants,in document order  var descendants = element.getDescendants();  for (var i =0; i < descendants.length; i++) {    var elem = descendants[i];    var type = elem.getType();    // We'll only examine ELEMENTs    if (type == XmlService.ContentTypes.ELEMENT) {      var element = elem.asElement();      var HTMLTag = element.getname();      if (HTMLTag === elementType) {        if (val === element.getAttribute(attr).getValue()) {          return element;        }      }    }  }  // No matches in document  return null;}

将此应用于您的示例,我们得到:

function getProgrammeList() {  txt = '<HTML> <body> <div> <div> <div ID="here">Hello World!!</div> </div> </div> </HTML>'  // Get the receIEved xml response into an XML document  var doc = XmlService.parse(txt);  var found = getdivByID(doc.getElement(),'here');  Logger.log(found.getAttribute(attr).getValue()               + " = "             + found.getValue());    /// here = Hello World!!}

注意:有关使用这些实用程序的实际示例,请参阅this answer.

总结

以上是内存溢出为你收集整理的dom – Google Apps脚本有类似getElementById的内容吗?全部内容,希望文章能够帮你解决dom – Google Apps脚本有类似getElementById的内容吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108052.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存