Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hi I'm trying to grab this webpage and store it into a table... any table. I'm using Google script.

var fetchString="http://www.airchina.com.cn/www/en/html/index/ir/traffic/"
var response = UrlFetchApp.fetch(fetchString);

I need some help on the code to get this started. I'm looking to grab the "Traffic Data" table. I would like to put it into an 2D array if possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
352 views
Welcome To Ask or Share your Answers For Others

1 Answer

Google provides a XML parsing/manipulating service. You can use this to parse the html that is in that table.

One note, if you investigate where that html is actually coming from, you'll see that it's actually coming from a different url. http://www.airchina.com.cn/www/jsp/airlines_operating_data/exlshow_en.jsp

So here's what I got for you. It works pretty well. Hopefully this is enough of a start for you.

function fetchIt() {
  var fetchString="http://www.airchina.com.cn/www/jsp/airlines_operating_data/exlshow_en.jsp"
  var response = UrlFetchApp.fetch(fetchString);

  var xmlDoc = Xml.parse(response.getBlob().getDataAsString(),true);
  var b = xmlDoc.getElement().getElement("body");
  var table = b.getElement("div").getElement("div").getElement("div").getElements("div")[1].getElement("table");

  var rows = [];
  var trs = table.getElements("tr");
  for (var r=0,rlength=trs.length; r<rlength; r++) {
    var tds = trs[r].getElements("td");
    var row = [];
    for (var c=0,clength=tds.length; c<clength; c++) {
      row.push(tds[c].getText());
    }
    rows.push(row);
  }

  Logger.log(Utilities.jsonStringify(rows));

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...