简体   繁体   English

从Nodejs应用程序加载HTML表

[英]Loading HTML Table from Nodejs app

From a nodejs app I want to load a web page from a url. 从nodejs应用程序,我想从URL加载网页。 I know that has a single html table. 我知道只有一个html表。 How do I iterate each row and column to traverse every value in that table? 如何遍历每一行和每一列以遍历该表中的每个值?

You'll probably need some middleware, request and cheerio could be used for this 您可能需要一些中间件, 为此可以使用requestcheerio

var request = require('request');
var cheerio = require('cheerio');

request('http://www.example.com/url/tofile.html', function (error, response, body) {
  if (!error && response.statusCode == 200) {

     var $     = cheerio.load(body);
     var table = $('table#withID');
     var tr    = table.find('tr');
     var res   = [];

     tr.each(function() {
         var txt = $(this).find('td').text();

         res.push(txt);
     });

  }
});

Have a look at https://www.npmjs.com/package/htmlparser2 看看https://www.npmjs.com/package/htmlparser2

Then catch onopentag for "td". 然后捕获onopentag为“ td”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM