【问题标题】:Scrape repetative HTML into JSON array with Node使用 Node 将重复的 HTML 抓取到 JSON 数组中
【发布时间】:2017-09-16 07:15:30
【问题描述】:

我正在练习抓取,我正在尝试将代理列表抓取到 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 行吗?看看它是否改变了什么
  • @VladHolubiev 什么都没有

标签: html node.js each cheerio


【解决方案1】:

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

所以发生的情况是,第一次,您在该对象中插入相关数据。你把对象推到数组上。

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

等等。

解决方法:每次创建一个新对象,只需移动这一行即可:

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

在循环内部。

更新

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

解决方案:通过将elem 作为第二个参数添加到这些调用中,指定您要相对于当前元素工作:$('.inCaption', elem)$('.inCaption', elem)

【讨论】:

  • 不幸的是,这似乎没有任何作用
  • 添加了第二个问题和解决方案。
  • 不错!谢谢,所以有两个问题。我确实在考虑第二个问题,但不知道到底发生了什么。
猜你喜欢
  • 1970-01-01
  • 2021-05-10
  • 2010-09-07
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2020-10-27
  • 2017-12-22
相关资源
最近更新 更多