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.

1 Answer 1

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

}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Phil, another question for you if I may. This page 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.
It's pretty simple. Please let me know if you don't follow what the code is doing. pastebin.com/4xUPDCz1
Can some one help me here
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.