【问题标题】:Array.push not pushing properlyArray.push 没有正确推送
【发布时间】:2016-12-27 12:42:42
【问题描述】:

编辑:我添加了 res.send(json),不包括 sn-p。

我正在尝试提取/抓取网站并将其输出组合到 json 数据中。但是,当我运行端点时,响应会将每个结果组合到它的键中,而不是每次迭代都添加一条记录。为了阐述,我得到:

{ 
      item: "item1item2item3item4item5item6",
      title: "title1title2title3title4title5title5",
      price: "price1price2price3price4price5price6"
}

虽然这是我的目标输出格式..:

{ item: "item1",
  title: "title1",
  price: "price1",
  itemlink: "itemlink1" },
{ item: "item2",
  title: "title2",
  price: "price2",
  itemlink: "itemlink2" },
{ item: "item3",
  title: "title3",
  price: "price3",
  itemlink: "itemlink3" }, etc...

这是下面的sn-p:

    request(url, function(error, response, html){
    if(!error){
        var $ = cheerio.load(html);

    var json = [];
    /* Pulls out all the titles
    $('.item-name').each(function() {
        var title = $(this).text();
        json2.push({title: title});
    })
    */

    function getID(str) {
        return str.split('viewitem.php?iid=')[1];
    }

    $('.catlist').each(function(key, index) {
        var title = $('.item-name').text();
        var price = $('.catprice').text();
        var id = getID($('h2').children().attr('href'));
        var itemlink = $('h2').children().attr('href');

        json.push({     
            id: id, 
            title: title, 
            price: price, 
            itemlink: itemlink 
        });
    })

}
res.send(json)

})

我疯了,已经花了好几个小时了。知道为什么他们没有为我正确迭代吗?提前致谢!

【问题讨论】:

  • 您是否尝试对此进行调查(...使用调试器)?
  • 您正在更改 json 的值,但没有将广告新值推送到数组中

标签: javascript jquery express npm cheerio


【解决方案1】:
 $('.catlist').each(function(key, index) {
    var title = $(this).find('.item-name').text();
    var price = $(this).find('.catprice').text();
    var id = getID($(this).find('h2').children().attr('href'));
    var itemlink = $(this).find('h2').children().attr('href');

    var temp = 
    {   
        id: id, 
        title: title, 
        price: price, 
        itemlink: itemlink 
    };
    json.push(temp);
})

您需要为每个.catlist 找到孩子并将它们一个一个推送到数组中

【讨论】:

  • 谢谢,“(this)”确实有帮助。起初,当我尝试您的修复时,我意识到我的 CSS 选择器只是选择了一个孩子。我将其更改为 "$('.catlist').find('li').each(function(key, index) {" 并且它起作用了。猜猜如果没有看到网站被刮掉,这很难提供帮助。;) 谢谢你的帮助。
  • 当您编写并了解更多信息时,您将构建自己的错误检测器。 :)
【解决方案2】:

正如您在代码中看到的,您在每次迭代中都为 json 赋值。所以这就是为什么它没有添加新记录的原因。 看看这段代码

 $('.catlist').each(function(key, index) {
        var title = $('.item-name').text();
        var price = $('.catprice').text();
        var id = getID($('h2').children().attr('href'));
        var itemlink = $('h2').children().attr('href');

        var temp = 
        {   
            id: id, 
            title: title, 
            price: price, 
            itemlink: itemlink 
        };
        json.push(temp);
    })

【讨论】:

    【解决方案3】:

    试试这个

     json.push( {
       id: id, 
       title: title, 
       price: price,
       itemlink: itemlink 
     });
    

    【讨论】:

      猜你喜欢
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 2021-08-02
      相关资源
      最近更新 更多