使用Node将重复的HTML写入JSON数组

时间:2017-04-19 21:17:03

标签: html node.js each cheerio

我正在练习抓取,我正在尝试将代理列表写入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;

1 个答案:

答案 0 :(得分:0)

第一个问题是您重复使用相同的json变量。

所以会发生的是第一次,您在该对象中插入相关数据。你按下阵列上的对象。

在下一次迭代中,您修改同一个变量(因此您更改了数组中已存在的变量,因为它是相同的变量),然后再次推送它。

等等。

解决方案:每次创建一个新对象,只需移动此行:

var json = { agent : "", agency : "", street : "", city : "", state : "", country : "", zip : "", deskphone : "", fax : "", email : "", cell : ""};

在循环中。

更新

第二个问题是您对$('.inCaption')$('.inCaption')的查找是相对于整个文档的,因此每次都会获得相同的结果(实际上是这些元素的列表)。

解决方案:指定您希望相对于当前元素工作,方法是将elem添加为这些调用的第二个参数:$('.inCaption', elem)$('.inCaption', elem)