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
  • 34,714
  • 9
  • 70
  • 166
jason
  • 3,811
  • 18
  • 92
  • 147

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,792
  • 1
  • 19
  • 27
  • 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
  • 1
    @PhilBozak I guess there was a time when this used to work. XML.parse() is no longer in google scripts. However, there is XMLService.parse, but it does to seem to work with my code. I am getting an error re: raquo that is on the site that i am trying to read. – Nation Jun 25 '22 at 02:26