Nicolas Albarracin
Nicolas Albarracin

Reputation: 217

Cant get image src with cheerio

Im trying to put togheder a scraper with nodejs and cheerio. i have this so far:

class ScraperService {

    static getDwelling(url) {
        const dwelling = {
            images: []
        };
        return new Promise((resolve, reject) => {
            request(`https://www.zonaprop.com.ar/propiedades/${url}`, (err, resp, html) => {
                if(err || resp.statusCode === 404) {
                    return reject(err);
                }
                const $ = cheerio.load(html);
                pe = $('.price-operation', '#article-container').text();
                dwelling.price = $('.price-items', '#article-container').text();
                dwelling.description = $('.description-container', '#article-container').html();
                //getting images here
                $('#tab-foto-flickity').find('img').each(() => {dwelling.images.push(this);});
                resolve(dwelling);
            });
        });
    }
}

module.exports = ScraperService;

the problem is im getting 37 nulls insted of the link the the images, i have tried different tags but no luck. Any ideas where is the code failing Code to scrape

Upvotes: 0

Views: 611

Answers (1)

amenzou
amenzou

Reputation: 319

The reason why you're getting nulls is due to the use of arrow functions () =>{} in the each callback. Arrow function do not have a this. Try using a classic anonymous function.

see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Upvotes: 1

Related Questions