2017-05-31 323 views
2
<table cellspacing="0" cellpadding="0" width="90%" align="center" border="0">  
     <tr> 
      <td> 

         <table cellpadding="0" cellspacing="1" width="90%" border="0" align="center"> 
          <tr> 
           <td 

整个第二个表格位于第一个表格的td标签内。Node.js Cheerio解析html表格内的html表格

我是新来的cheerio。我不能完全得到我的输出,只给出了内部表tr的值。我得到两张桌子,这很混乱。

$ = cheerio.load(html.toString()); 
var data = []; 
    $('tr').each(function(i, tr){ 

     var children = $(this).children(); 
     var itemNum = children.eq(0); 

     var row = { 
      "Num": itemNum.text().trim() 
     }; 
     data.push(row); 
     console.log(row); 
    });  
+0

任何帮助表示赞赏 – user3911976

回答

1
$ = cheerio.load(html.toString()); 
var data = []; 
    $('table tr td table tr').each(function(i, td){ 

     var children = $(this).children(); 
     var itemNum = children.eq(0); 
     var itemName = children.eq(1); 

此代码固定的问题。我没有意识到你只是通过元素,直到你得到你需要的那个然后使用children.eq(n)我能够获得行内的每个td文本值。希望这可以帮助别人。