Google Apps Script 是否有类似 getElementById 的内容?

ben*_*ung 5 dom web-scraping google-apps-script

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

Mog*_*dad 6

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


查看教程:解析 XML 文档。(截至 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
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

var programmeList = findDivById(doc,"here");
Run Code Online (Sandbox Code Playgroud)

编辑-我无法控制自己......

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

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

将其应用到您的示例中,我们得到:

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!!
}
Run Code Online (Sandbox Code Playgroud)

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