这是情况。我正在使用 Node.js 和 Cheerio 我的控制台日志返回很好,除了站点结构导致的一些重复内容......
我的脚本:
var request = require ('request'),
cheerio = require('cheerio'),
chart = [];
request('http://www.website-X.com', function(err, resp, body){
if(!err && resp.statusCode == 200){
var $ = cheerio.load(body);
$('tr', '#chart_body').each(function(){
var rank = $(this).text().trim().replace(/\s\s+/g, ';');
chart.push(rank);
});
console.log(chart);
}
});
网站结构(简化):
<table id="chart_body">
<tr><!-- 1 Info I need --></td>
<tr><!-- 2 Info I need --></td>
<table>
<tbody>
<tr> Duplicate info as 1 </tr>
</tbody>
</table>
<tr><!-- 3 Info I need --></td>
<tr><!-- 4 Info I need --></td>
<tr><!-- 5 Info I need --></td>
<tr><!-- 6 Info I need --></td>
</table>
我的控制台日志返回:
'1;Wolfenstein;330,703;330,703;1',
'Wolfenstein',
'2;Wolfenstein;188,200;188,200;1',
'Wolfenstein',
'3;Minecraft;126,041;215,109;2',
'Minecraft','
我的控制台日志返回很好,除了重复的东西。这是因为在站点结构中,选择器 tr 在其中有另一个 tr。我无法摆脱'tr tr'。tr 也没有可供进一步选择的独特类。
请帮忙。谢谢!!!-阿尔多
哦,最后......每次返回的开头和结尾的讨厌的单引号。我拿不出来