4

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.

Rubén
  • 29,320
  • 9
  • 61
  • 145
jason
  • 2,953
  • 16
  • 80
  • 130

1 Answers1

7

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));

}
Phil Bozak
  • 2,765
  • 1
  • 18
  • 27
  • sweet. Thanks for the help! I'll take a look at the XML tool you referred to. – jason Feb 11 '13 at 06:40
  • Hi Phil, another question for you if I may. This page http://www.bloomberg.com/markets/companies/country/hong-kong/ ... I want to put the info on that page into a table. The changes are throwing me off. Could you lend me a hand again? thanks. – jason May 31 '13 at 10:33
  • It's pretty simple. Please let me know if you don't follow what the code is doing. http://pastebin.com/4xUPDCz1 – Phil Bozak Jun 01 '13 at 16:29
  • Can some one help me [here](http://stackoverflow.com/questions/23874791/grab-table-from-url-in-google-app-script) – Vasim Jun 10 '14 at 07:21