简体   繁体   English

为什么我的数组被覆盖而不是使其成为一个包含对象的数组?

[英]Why is my array overwriting instead of making it one array with objects inside?

I've been working on scraper function.我一直在研究刮刀 function。

Now data is fetched from this component:现在从此组件中获取数据:

const cheerio = require('cheerio');
const axios = require('axios');
const parsing = require('./parseData');

exports.olxScraper = () => {
  const url =
    'https://www.olx.ba/pretraga?vrsta=samoprodaja&kategorija=23&sort_order=desc&kanton=9&sacijenom=sacijenom&stranica=1';

  const getRawData = async () => {
    try {
      await axios.get(url).then((res) => {
        const $ = cheerio.load(res.data);
        $('div[id="rezultatipretrage"] > div')
          .not('div[class="listitem artikal obicniArtikal  i index"]')
          .not('div[class="obicniArtikal"]')
          .each((index, element) => {
            $('span[class="prekrizenacijena"]').remove();
            const getLink = $(element)
              .find('div[class="naslov"] > a')
              .attr('href');
            const getDescription = $(element)
              .find('div[class="naslov"] > a > p')
              .text();
            const getPrice = $(element)
              .find('div[class="datum"] > span')
              .text()
              .replace(/\.| ?KM$/g, '')
              .replace(' ', '');
            const getPicture = $(element)
              .find('div[class="slika"] > img')
              .attr('src');
            parsing.parseData(getLink, getDescription, getPrice, getPicture);
          });
      });
    } catch (error) {
      console.log(error);
    }
  };
  getRawData();
};

exports.santScraper = () => {
  const url = `https://www.sant.ba/pretraga/prodaja-1/tip-2/cijena_min-20000/stranica-1`;

  const getRawData = async () => {
    try {
      await axios.get(url).then((response) => {
        const $ = cheerio.load(response.data);

        $('div[class="col-xxs-12 col-xss-6 col-xs-6 col-sm-6 col-lg-4"]').each(
          (index, element) => {
            const getLink = $(element).find('a[class="re-image"]').attr('href');
            const getDescription = $(element).find('a[class="title"]').text();
            const getPrice = $(element)
              .find('div[class="prices"] > h3[class="price"]')
              .text()
              .replace(/\.| ?KM$/g, '')
              .replace(',', '.');
            const getPicture = $(element).find('img').attr('data-original');
            
            parsing.parseData(getLink, getDescription, getPrice, getPicture);
          }
        );
      });
    } catch (error) {
      console.log(console.log(error));
    }
  };
  getRawData();
};

this.olxScraper();
this.santScraper();

So scraped that is sent to parseData component which looks like this:因此,将其发送到 parseData 组件,如下所示:

exports.parseData = (getLink, getDescription, getPrice, getPicture) => {
  const apartments = [];
  let apartment = {
    link: getLink,
    descr: getDescription,
    price: getPrice,
    picture: getPicture,
  };
  apartments.push(apartment);
  console.log(apartments);
};

And the output is this: output 是这样的:

[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3467-dvosoban-stan-sa-liftom-u-samom-centru-grada-63-m2',
    descr: ' dvosoban stan sa liftom u samom centru grada , 63 m2 ',
    price: '199000.00',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3467/3467_1_1563271355.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3689-troiposoban-stan-u-naselju-sunca',
    descr: ' Troiposoban stan u naselju Sunca ',
    price: '360000.00',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3689/3689_1_1612344465.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3677-trosoban-renoviran-stan-u-naselju-dolac-malta-73-m2',
    descr: ' trosoban, renoviran stan u naselju Dolac malta, 73 m2 ',
    price: '150000.00',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3677/3677_1_1608550332.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3537-cetverosoban-stan-u-novogradnji-sa-prelijepim-pogledom-na-grad-118-38-m2',
    descr: ' Četverosoban stan u novogradnji sa prelijepim pogledom na grad, 118,38 m2 ',
    price: '538090.37',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3537/3537_1_1573217155.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3536-trosoban-stan-u-novogradnji-sa-prelijepim-pogledom-na-grad-105-34-m2',
    descr: ' Trosoban stan u novogradnji sa prelijepim pogledom na grad, 105,34 m2 ',
    price: '478817.70',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3536/3536_2_1573216638.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3535-luksuzni-cetverosoban-stan-na-cobaniji-117-43m2',
    descr: ' Luksuzni četverosoban stan na Čobaniji,117,43m2 ',
    price: '533772.19',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3535/3535_13_1573215220.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3534-trosoban-stan-u-novogradnji-90-m2',
    descr: ' trosoban stan u novogradnji, 90 m2 ',
    price: '376923.46',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3534/3534_8_1573214216.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3524-dvosoban-stan-u-naselju-bjelave-55-m2',
    descr: ' dvosoban stan u naselju Bjelave ,55 m2 ',
    price: '150000.00',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3524/3524_1_1571224667.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.sant.ba/nekretnine/nekretnina-3665-novogradnja-dvosoban-stan-skenderija-72-66-m2',
    descr: ' novogradnja, dvosoban stan,Skenderija, 72,66 m2 ',
    price: '290640.00',
    picture: 'https://www.sant.ba/thumb.php?file=photos/3665/3665_1_1606393537.jpg&maxw=273&maxh=205'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40528561/my-space-stan-stari-grad-renoviran-namjesten-50m2/',
    descr: 'MY SPACE/ Stan/ Stari Grad/ RENOVIRAN/ NAMJESTEN/ 50m2',
    price: '195000',
    picture: 'https://s9.pik.ba/galerija/2020-11/14/12/slika-1070163-5fafc62c13a67-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/39513168/cetverosoban-stan-u-novogradnji-malta-complex/',
    descr: 'Četverosoban stan u novogradnji "Malta Complex"',
    price: '166354',
    picture: 'https://s9.pik.ba/galerija/2020-08/25/12/slika-2044886-5f44e95ad18a8-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41817042/my-space-stan-cengic-vila-gradacacka-48-m2/',
    descr: 'MY SPACE/ Stan/ Cengic Vila/ Gradacacka/ 48 m2',
    price: '120000',
    picture: 'https://s9.pik.ba/galerija/2021-02/15/02/slika-1070163-602a78b066043-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41816059/adaptiran-trosoban-stan-2-sprat-dolac-malta-socijalno/',
    descr: 'Adaptiran trosoban stan / 2. sprat / Dolac Malta / Socijalno',
    price: '139000',
    picture: 'https://s9.pik.ba/galerija/2021-02/15/01/slika-1424529-602a69c8c218f-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41042243/renovirana-garsonjera-na-skenderiji-prodaja/',
    descr: 'Renovirana garsonjera na Skenderiji, prodaja!',
    price: '110000',
    picture: 'https://s9.pik.ba/galerija/2020-12/22/09/slika-115390-5fe1aee8796d0-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/34234631/cetverosoban-stan-3b-novogradnja-vrtovi-riverine-ilidza/',
    descr: 'Četverosoban stan-3B-NOVOGRADNJA/VRTOVI RIVERINE/ILIDŽA',
    price: '156692',
    picture: 'https://s9.pik.ba/galerija/2021-01/27/01/slika-1269653-6011595b4e941-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41776858/my-space-stan-dolac-malta-paromlinska-55-m2/',
    descr: 'MY SPACE/ Stan/ Dolac malta/ Paromlinska/ 55 m2',
    price: '139000',
    picture: 'https://s9.pik.ba/galerija/2021-02/12/03/slika-1070163-602697ad1561f-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40854733/prostor-prodaje-dvosoban-stan-alipasino-polje/',
    descr: 'PROSTOR prodaje: Dvosoban stan, Alipašino Polje',
    price: '104500',
    picture: 'https://s9.pik.ba/galerija/2020-12/08/05/slika-48261-5fcfaf984084d-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/39192105/stan-102-m2-dva-balkona-sa-kuhinjom-stup/',
    descr: 'Stan 102 m2 Dva balkona, sa kuhinjom Stup',
    price: '230000',
    picture: 'https://s5.pik.ba/galerija/2019-06/11/11/slika-925242-5cff6dd8594b0-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41136078/prostor-prodaje-dvosoban-stan-blizu-bascarsije/',
    descr: 'PROSTOR prodaje: Dvosoban stan blizu Baščaršije',
    price: '153000',
    picture: 'https://s9.pik.ba/galerija/2020-12/29/03/slika-48261-5feb3ec26181a-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40102853/my-space-lux-stan-hrasno-bosmal-155-m2/',
    descr: 'MY SPACE/ LUX Stan/ Hrasno/ Bosmal/ 155 m2',
    price: '619000',
    picture: 'https://s9.pik.ba/galerija/2020-10/12/03/slika-1070163-5f8460248bc5a-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41712867/my-space-stan-centar-mejtas-buka-95-m2/',
    descr: 'MY SPACE/ Stan/ Centar/ MEJTAS/ Buka/ 95 m2',
    price: '300000',
    picture: 'https://s9.pik.ba/galerija/2021-02/08/01/slika-1070163-60213553ae220-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/32083654/prodaja-stanova-novogradnja-vrtovi-riverine-ilidza/',
    descr: 'PRODAJA STANOVA-NOVOGRADNJA/VRTOVI RIVERINE/ILIDŽA',
    price: '75284',
    picture: 'https://s9.pik.ba/galerija/2020-08/26/11/slika-1269653-5f4627de84c78-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/34059753/cetverosoban-stan-3a-novogradnja-vrtovi-riverine-ilidza/',
    descr: 'Četverosoban stan-3A-NOVOGRADNJA/VRTOVI RIVERINE/ILIDŽA',
    price: '162776',
    picture: 'https://s9.pik.ba/galerija/2020-08/26/11/slika-1269653-5f4626b70c55d-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40667077/stan-sarajevo-stari-grad-bistrik-detaljni-opis/',
    descr: 'Stan Sarajevo - Stari Grad - Bistrik Detaljni opis',
    price: '207000',
    picture: 'https://s5.pik.ba/galerija/2019-08/13/07/slika-295630-5d52fa9a44bda-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41815741/my-space-stan-ilidza-grand-centar-54m2/',
    descr: 'MY SPACE/ Stan/ Ilidza/ Grand centar/ 54m2',
    price: '135000',
    picture: 'https://s9.pik.ba/galerija/2021-02/15/01/slika-1070163-602a65e1dde3a-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40980442/prodaje-se-dvosoban-stan-kosevo/',
    descr: 'PRODAJE SE dvosoban stan- Koševo',
    price: '230000',
    picture: 'https://s9.pik.ba/galerija/2020-12/17/03/slika-154243-5fdb6eaea9094-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/34878314/prodaje-se-stan-na-stupu-ul-pijacna/',
    descr: 'PRODAJE SE stan na Stupu -ul. Pijačna',
    price: '92000',
    picture: 'https://s5.pik.ba/galerija/2019-08/19/04/slika-154243-5d5ab3f0bf95e-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41676279/on-time-prodaje-marijin-dvor-stan-86-m2/',
    descr: 'ON TIME prodaje: Marijin Dvor, Stan, 86 m2',
    price: '387000',
    picture: 'https://s9.pik.ba/galerija/2021-02/05/09/slika-2438228-601daa8830fb3-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40870310/prostor-prodaje-garsonjera-30m2-tibra-nedzarici/',
    descr: 'PROSTOR prodaje: Garsonjera 30m2, Tibra Nedžarići',
    price: '64500',
    picture: 'https://s9.pik.ba/galerija/2020-12/09/06/slika-48261-5fd10627a5706-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41516526/prostor-prodaje-dvoetazni-stan-aerodromsko-naselje/',
    descr: 'PROSTOR prodaje: Dvoetažni stan, Aerodromsko naselje',
    price: '195000',
    picture: 'https://s9.pik.ba/galerija/2021-01/26/05/slika-48261-601044ea62fb1-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/38022587/dvosoban-stan-stup-tibra-49-m2/',
    descr: 'Dvosoban stan Stup Tibra 49 m2',
    price: '97000',
    picture: 'https://s5.pik.ba/galerija/2020-05/06/02/slika-1718896-5eb2b0e7d94e4-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41733041/trosoban-stan-nova-otoka-novogradnja-60m/',
    descr: 'Trosoban stan Nova Otoka/Novogradnja/60m²',
    price: '175000',
    picture: 'https://s9.pik.ba/galerija/2021-02/09/06/slika-1718896-6022bfc8254f1-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41714425/dvosoban-namjesten-stan-otoka-miljacka-novogradnja/',
    descr: 'Dvosoban namjesten stan Otoka/Miljacka/ Novogradnja',
    price: '142000',
    picture: 'https://s5.pik.ba/galerija/2020-05/09/04/slika-1718896-5eb6bcb7f0397-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41732985/trosoban-stan-nova-otoka-novogradnja-74m/',
    descr: 'Trosoban stan Nova Otoka/Novogradnja/ 74m²',
    price: '218000',
    picture: 'https://s9.pik.ba/galerija/2021-02/09/06/slika-1718896-6022c1db4b5f9-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41440791/prostor-prodajetrosoban-stan-sa-balkonom-i-lodjom-stup/',
    descr: 'PROSTOR prodaje:Trosoban stan sa balkonom i lođom, Stup',
    price: '126500',
    picture: 'https://s9.pik.ba/galerija/2021-01/21/04/slika-48261-6009983ee116c-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40039060/novogradnja-buca-potok-dvosoban-stan-47-60m2popust-3/',
    descr: 'NOVOGRADNJA BUĆA POTOK! Dvosoban stan 47,60m2!Popust 3%',
    price: '119000',
    picture: 'https://s9.pik.ba/galerija/2020-10/22/05/slika-48261-5f91a08c623ce-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/41449900/novogradnja-stup-funkcionalan-trosoban-stan-48m2/',
    descr: 'NOVOGRADNJA Stup: Funkcionalan trosoban stan, 48m2',
    price: '103849',
    picture: 'https://s9.pik.ba/galerija/2021-01/22/10/slika-48261-600a9b8c3b3f1-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/39714781/novogradnja-buca-potok-dvosoban-stan-62-35-m2/',
    descr: 'NOVOGRADNJA BUĆA POTOK! Dvosoban stan 62,35 m2!',
    price: '149546',
    picture: 'https://s9.pik.ba/galerija/2020-10/22/05/slika-48261-5f91ab463dc5e-thumb.jpg'
  }
]
[
  {
    link: 'https://www.olx.ba/artikal/40743429/novogradnja-stup-komforan-dvosoban-stan-56m2-sarajevo/',
    descr: 'NOVOGRADNJA Stup: Komforan dvosoban stan 56m2, Sarajevo',
    price: '124051',
    picture: 'https://s9.pik.ba/galerija/2020-11/30/05/slika-48261-5fc51924c75f9-thumb.jpg'
  }
]

Now this isn't output that I expected.现在这不是我预期的 output。 I've expect array of objects not array with single object, and arrays keep rewriting itself one by one.我期望对象数组不是带有单个 object 的数组,并且 arrays 会不断地重写自己。

How would I make it just be one array with objects inside it?我如何使它成为一个包含对象的数组? And also why is this happening?还有为什么会这样? Thanks!谢谢!

EDIT Here is what I've managed to do with help of one answer: parseData component编辑这是我在一个答案的帮助下设法做到的: parseData 组件

const apartments = [];
exports.parseData = (getLink, getDescription, getPrice, getPicture) => {
  let apartment = {
    link: getLink,
    descr: getDescription,
    price: getPrice,
    picture: getPicture,
  };
  apartments.push(apartment);
  console.log(apartments.length);
};

Now the length output is like this: 1 2 3 4 5 etc. LastOne where I expected to length be just last number of array length.现在长度 output 是这样的: 1 2 3 4 5 etc. LastOne我希望长度只是数组长度的最后一个数字。 So again, it's rewriting array until last one object is pushed into array.因此,它再次重写数组,直到最后一个 object 被推入数组。

Here problem is when you call this method ' parseData() ' at that time new " apartments " array is created and you pushed in array so each time when you call function that time new array is created so you didn't get array of objects.这里的问题是,当您调用此方法“ parseData() ”时,会创建新的“公寓”数组并推入数组,因此每次调用 function 时都会创建新数组,因此您没有获得对象数组.

Solution: If you want array of objects for that you have to declare your array out of the " parseData() ".解决方案:如果您想要对象数组,则必须从“ parseData() ”中声明您的数组。

Also you can do something like this.你也可以做这样的事情。

apartments.push({ link: getLink, descr: getDescription, price: getPrice, picture: getPicture, });公寓.push({链接:getLink,描述:getDescription,价格:getPrice,图片:getPicture,});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM