Google Apps Script 是否具有类似 getElementById 的功能? [英] Does Google Apps Script have something like getElementById?

查看:30
本文介绍了Google Apps Script 是否具有类似 getElementById 的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用 Google App Script 从电台网站获取节目列表.如何通过指定元素的id来选择网页中指定的元素?因此,我可以获取网页中的程序.

解决方案

编辑,2013 年 12 月:Google 已弃用旧的 Xml 服务,将其替换为 XmlService.此答案中的脚本已更新为使用新服务.新服务需要符合标准的 XML &HTML,而旧的则宽容了诸如缺少关闭标签之类的问题.

<小时>

查看教程:解析 XML 文档.(截至 2013 年 12 月,本教程仍然在线,尽管 Xml 服务已弃用.)从该基础开始,您可以利用 Script Services 中的 XML 解析来导航页面.这是一个在您的示例上运行的小脚本:

function getProgrammeList() {txt = '<身体><div><div><div id="here">hello world!!</div>

</html>'//将接收到的 xml 响应转换为 XMLdocument 格式var doc = Xml.parse(txt,true);Logger.log(doc.html.body.div.div.div.id +" = "+doc.html.body.div.div.div.Text );///这里=你好世界!!调试器;//在调试器中暂停 - 检查文档的内容}

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

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

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

var programList = findDivById(doc,"here");

<小时>

编辑 - 我情不自禁...

这是一个实用函数,可以做到这一点.

/*** 找到一个<div>带有给定 id 的标签.* <预>* 示例:getDivById( html, 'tagVal' ) 会找到** 

* </pre>** @param {元素|文档}* 要开始搜索的元素 XML 文档或元素.* @param {String} id HTML

要查找的 ID.** @return {XmlElement} 第一个匹配元素(按文档顺序)或 null.*/函数 getDivById( 元素, id ) {//调用实用函数来完成这项工作.return getElementByVal( element, 'div', 'id', id );}/*** !现在更新了 XmlService!** 遍历给定的 Xml 文档或元素以寻找匹配项.* 注意:'class' 在解析过程中被剥离,不能用于* 搜索,我不知道为什么.* <预>* 示例:getElementByVal( body, 'input', 'value', 'Go' );会发现** <input type="submit" name="btn" value="Go" id="btn" class="submit buttonGradient"/>* </pre>** @param {元素|文档}* 要开始搜索的元素 XML 文档或元素.* @param {String} elementType XML 元素类型,例如'div' 表示 <div>* @param {String} attr 要比较的属性或属性.* @param {String} val 搜索值来定位** @return {Element} 第一个匹配元素(按文档顺序)或 null.*/函数 getElementByVal( 元素, elementType, attr, val ) {//获取所有后代,按文档顺序var后代 = element.getDescendants();for (var i =0; i

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

function getProgrammeList() {txt = '<身体><div><div><div id="here">hello world!!</div>

</html>'//将接收到的 xml 响应转化为 XML 文档var doc = XmlService.parse(txt);var found = getDivById(doc.getElement(),'here');Logger.log(found.getAttribute(attr).getValue()+ " = "+ found.getValue());///这里=你好世界!!}

注意:请参阅此答案 有关使用这些实用程序的实际示例.

I am gonna to use Google App Script to fetch the programme list from the website of radio station. How can I select the specified elements in the webpage by specifying the id of the element? Therefore, I can get the programs in the webpage.

解决方案

Edit, Dec 2013: Google has deprecated the old Xml service, replacing it with XmlService. The script in this answer has been updated to use the new service. The new service requires standard-compliant XML & HTML, while the old one was forgiving of such problems as missing close-tags.


Have a look at the Tutorial: Parsing an XML Document. (As of Dec 2013, this tutorial is still on line, although the Xml service is deprecated.) Starting with that foundation, you can take advantage of the XML parsing in Script Services to navigate the page. Here's a small script operating on your example:

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
}

To get the real page, start with this:

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

If you look at the documentation for getElements you'll see that there is support for retrieving specific tags, for example "div". That finds direct children of a specific element, it doesn't explore the entire XML document. You should be able to write a function that traverses the document examining the id of each div element until it finds your programme list.

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


Edit - I couldn't help myself...

Here's a utility function that will do just that.

/**
 * 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" class="submit buttonGradient" />
 * </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;
}

Applying this to your example, we get this:

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!!
}

Note: See this answer for a practical example of the use of these utilities.

这篇关于Google Apps Script 是否具有类似 getElementById 的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆