LoginSignup
3
3

More than 3 years have passed since last update.

GASによるスクレイピングのために作った機能

Last updated at Posted at 2019-10-11

Google Apps Scriptでスクレイピングをし易くするために、フロントエンドの方にお馴染みのDOM機能の一部のようなプログラムを作成。


// XmlServiceを前提としたScrapingお助けProgram

/*
* Method: getElementById
* @param {XmlService.Element} element
* @param {string} idToFind
* @return {XmlService.Element} 
* Description: if id does not exist, null will return.
*/
function getElementById(element, idToFind) { 
  var descendants = element.getDescendants();
  for (i in descendants) {
    var elmt = descendants[i].asElement();
    if (elmt != null){
      var id = elmt.getAttribute('id')
      if (id != null) {
        if (idToFind === id.getValue()){
          return elmt;
        }
      }
    }
  }
}

/*
* Method: getElementsByClassName
* @param {XmlService.Element} element
* @param {string} classToFind
* @return {Array XmlService.Element[]}
* Description: if id does not exist, empty Array will return.
*/
function getElementsByClassName(element, classToFind){
  var classes = [];
  var descendants = element.getDescendants();
  for (i in descendants) {
    var elmt = descendants[i].asElement();
    if (elmt != null){
      var class = elmt.getAttribute('class');
      if (class != null) {
        if (classToFind === class.getValue()){
          classes.push(elmt.asElement());
        }
      }
    }
  }
  return classes;
}

/*
* Method: getElementsByTagName
* @param {XmlService.Element} element
* @param {string} tagToFind
* @return {Array XmlService.Element[]}
* Description: if id does not exist, empty Array will return.
*/
function getElementsByTagName(element, tagToFind){
  var tags = []
  var descendants = element.getDescendants();
  for (i in descendants) {
    var elmt = descendants[i].asElement();
    if (elmt != null) {
      var tag = elmt.getName();
      if (tag != null) {
        if (tagToFind === tag) {
          tags.push(elmt);
        }
      }
    }
  }
  return tags;
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3