最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - grabbing table from html using Google script - Stack Overflow

programmeradmin0浏览0评论

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

var fetchString="/"
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.

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..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.

Share Improve this question edited Jun 23, 2017 at 15:38 Wicket 38.4k9 gold badges78 silver badges193 bronze badges asked Feb 11, 2013 at 3:38 jasonjason 4,47923 gold badges103 silver badges153 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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 ing from, you'll see that it's actually ing from a different url. http://www.airchina..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..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));

}
发布评论

评论列表(0)

  1. 暂无评论