Finding elements after a URLfetch

2019-05-31 00:58发布

问题:

What would be the best way to parse an HTML string after a URLfetch ? If we find element it would return True else False, right now I'm using this , but I don't think I'm on the right path since it's already erroring out ! This is running as a script in a google spreadsheet .

    function amazon() {

    var response = UrlFetchApp.fetch("http://www.amazon.com/");
    var text = response.getContentText();

    var result = text.find("kindle");
    return result


    }

回答1:

You could try something like this, which would return false if the string "kindle" isn't present or true if it is present at least once.

function amazon() {
  var response = UrlFetchApp.fetch("http://www.amazon.com/");
  var text = response.getContentText();
  var result = text.search("kindle");

  if (result == -1) {
    return false;
  } else {
    return true;
  }
}