Я практикую очистку и пытаюсь очистить список агентов в массиве JSON. Мой код в настоящее время очищает последнего человека только 4 раза. Мне интересно, как перебирать каждый класс, который повторяется.
var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var app = express();
app.get('/scrape', function(req, res){
char = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x',
'y','z']
url = 'http://www.nhlpa.com/inside-nhlpa/certified-player-agents/find-an-agent?ln=A';
request(url, function(error, response, html){
if(!error){
var $ = cheerio.load(html);
var agent, agency, address, street, city, state, country, zip, deskphone, fax, email, cell;
var json = { agent : "", agency : "", street : "", city : "", state : "", country : "", zip : "", deskphone : "", fax : "", email : "", cell : ""};
var jsonarry = []
$('.inBox').each(function(i, elem) {
$('.inBodyText').filter(function(){
var data = $(this);
agent = data.children().first().text();
//agency = data.children().last().children().text();
json.agent = agent;
})
$('.inCaption').filter(function(){
var data = $(this);
agency = data.children().children().first().next().text();
json.agency = agency;
street = data.children().children().first().next().next().text();
json.street = street;
address = data.children().children().first().next().next().next().text().replace(/ /g,'');
address = address.split(",");
json.city = address[0];
json.state = address[1]
json.country = address[2]
zip = data.children().children().first().next().next().next().next().text();
json.zip = zip
deskphone = data.children().children().last().prev().prev().prev().text();
json.deskphone = deskphone
fax = data.children().children().last().prev().prev().text();
json.fax = fax
email = data.children().children().last().prev().text();
json.email = email
cell = data.children().children().last().text();
json.cell = cell
})
jsonarry.push(json)
});
}
fs.writeFile('output.json', JSON.stringify(jsonarry, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})
res.send(html)
}) ;
})
app.listen('8081')
console.log('Listen on port 8081');
exports = module.exports = app;
jsonarry.push(json)
всего на 1 строку вверх? и посмотреть, изменит ли это что-нибудь - person Vlad Holubiev   schedule 20.04.2017