报废物品如何分类?
How to sort scraped items?
我这里有工作抓取器,但我正在努力对数据进行排序。
这里是抓取代码:
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
http.createServer(function (req, res) {
request('http://www.xscores.com/soccer', function (error, response,
html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var list_items = "";
var arr = [];
var arr2 = [];
var j = 1;
$('div.match_line.score_row.other_match.e_true').each(function (i, element) {
var a = $(this).attr('data-home-team');
arr.push(a + " Row Number " + j);
j = j + 2;
//list_items += "<li>" + a + "</li>";
//console.log(arr.length);
});
var j = 2;
$('div.match_line.score_row.other_match.o_true').each(function (i, element) {
var b = $(this).attr('data-home-team');
arr.push(b + " Row Number " + j);
j = j + 2;
//list_items += "<li>" + b + "</li>";
//console.log(arr.length);
});
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
list_items += "<li>" + arr[i] + "</li>";
}
var html = "<ul>" + list_items + "</ul>"
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(html);
console.log(arr.length);
}
});
}).listen(8080);
console.log('Server is running at http://178.62.253.206:8080/');
所以问题来了,对这些元素进行排序。
- 元素'div.match_line.score_row.other_match.e_true'每n次放置一次
源页面中的奇数行号。
- 元素'div.match_line.score_row.other_match.o_true'在源页面中每隔n个偶数行放置一次。
现在排序的方式
- 我的数组(arr)首先将所有行的数据从第一行推入数组
元素(奇数行)
- 然后 array(arr) 将第二个元素的所有行数据推入同一个
大批。 (偶数行号)
所以基本上我数组中的项目是这样排序的 1,3,5,7,9,2,4,6,8(缩短)
如何排序才能以正确的顺序输出?
如有任何解决方案建议,我们将不胜感激
弗雷德里克
检查下面的arr.sort
方法。
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
http.createServer(function(req, res) {
request('http://www.xscores.com/soccer', function(error, response,
html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var list_items = "";
var arr = [];
var arr2 = [];
var j = 1;
$('div.match_line.score_row.other_match.e_true').each(function(i, element) {
var a = $(this).attr('data-home-team');
arr.push({
html: a,
j: j
});
j = j + 2;
//list_items += "<li>" + a + "</li>";
//console.log(arr.length);
});
var j = 2;
$('div.match_line.score_row.other_match.o_true').each(function(i, element) {
var b = $(this).attr('data-home-team');
arr.push({
html: b,
j: j
});
j = j + 2;
//list_items += "<li>" + b + "</li>";
//console.log(arr.length);
});
arr.sort(function(a, b) {
return a.j - b.j
})
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
list_items += "<li>" + arr[i].html + "</li>";
}
var html = "<ul>" + list_items + "</ul>"
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(html);
console.log(arr.length);
}
});
}).listen(8080);
console.log('Server is running at http://178.62.253.206:8080/');
我这里有工作抓取器,但我正在努力对数据进行排序。
这里是抓取代码:
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
http.createServer(function (req, res) {
request('http://www.xscores.com/soccer', function (error, response,
html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var list_items = "";
var arr = [];
var arr2 = [];
var j = 1;
$('div.match_line.score_row.other_match.e_true').each(function (i, element) {
var a = $(this).attr('data-home-team');
arr.push(a + " Row Number " + j);
j = j + 2;
//list_items += "<li>" + a + "</li>";
//console.log(arr.length);
});
var j = 2;
$('div.match_line.score_row.other_match.o_true').each(function (i, element) {
var b = $(this).attr('data-home-team');
arr.push(b + " Row Number " + j);
j = j + 2;
//list_items += "<li>" + b + "</li>";
//console.log(arr.length);
});
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
list_items += "<li>" + arr[i] + "</li>";
}
var html = "<ul>" + list_items + "</ul>"
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(html);
console.log(arr.length);
}
});
}).listen(8080);
console.log('Server is running at http://178.62.253.206:8080/');
所以问题来了,对这些元素进行排序。
- 元素'div.match_line.score_row.other_match.e_true'每n次放置一次 源页面中的奇数行号。
- 元素'div.match_line.score_row.other_match.o_true'在源页面中每隔n个偶数行放置一次。
现在排序的方式
- 我的数组(arr)首先将所有行的数据从第一行推入数组 元素(奇数行)
- 然后 array(arr) 将第二个元素的所有行数据推入同一个 大批。 (偶数行号)
所以基本上我数组中的项目是这样排序的 1,3,5,7,9,2,4,6,8(缩短)
如何排序才能以正确的顺序输出?
如有任何解决方案建议,我们将不胜感激
弗雷德里克
检查下面的arr.sort
方法。
var http = require('http');
var request = require('request');
var cheerio = require('cheerio');
http.createServer(function(req, res) {
request('http://www.xscores.com/soccer', function(error, response,
html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var list_items = "";
var arr = [];
var arr2 = [];
var j = 1;
$('div.match_line.score_row.other_match.e_true').each(function(i, element) {
var a = $(this).attr('data-home-team');
arr.push({
html: a,
j: j
});
j = j + 2;
//list_items += "<li>" + a + "</li>";
//console.log(arr.length);
});
var j = 2;
$('div.match_line.score_row.other_match.o_true').each(function(i, element) {
var b = $(this).attr('data-home-team');
arr.push({
html: b,
j: j
});
j = j + 2;
//list_items += "<li>" + b + "</li>";
//console.log(arr.length);
});
arr.sort(function(a, b) {
return a.j - b.j
})
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
list_items += "<li>" + arr[i].html + "</li>";
}
var html = "<ul>" + list_items + "</ul>"
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(html);
console.log(arr.length);
}
});
}).listen(8080);
console.log('Server is running at http://178.62.253.206:8080/');