How to capture class attribute with Cheerio?

Asked

Viewed 76 times

1

I’m studying Web Scrapping with Cheerio and Request. I’m having a hard time trying to capture the class attribute on the Amazon site that contains a div list and is not returning anything.

var Crawler = {
    request : null,
    cheerio : null,
    init : function(){
        Crawler.request = require('request');
        Crawler.cheerio = require('cheerio');
        Crawler.getProducts();
    },
    getProducts: function(){
        Crawler.request('https://www.amazon.com.br/s?k=notebook&__mk_pt_BR=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=13P0U6JH9AMHS&sprefix=%2Caps%2C416&ref=nb_sb_ss_i_1_1', function(err, res, body){
            if(err) console.log('Error: ' + err);

            var $ = Crawler.cheerio.load(body);

            $('div.s-result-list.s-search-results.sg-row div').each(function(){
                console.log("capturou");
            });
        });
    }
};
Crawler.init();

What am I doing wrong ?

1 answer

0


Missing to inform attribute gzip in his request:

Crawler.request({
  gzip: true,
  method: 'GET',
  url: 'https://www.amazon.com.br/s?k=notebook&__mk_pt_BR=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=13P0U6JH9AMHS&sprefix=%2Caps%2C416&ref=nb_sb_ss_i_1_1',
}, function (err, res, body) {
  // Execucação ...
});

gzip

if true, add an Accept-Encoding header to request Compressed content encodings from the server (if not already present) and Decode supported content encodings in the Response.

In free translation:

If true, adds a header Accept-Encoding to request server compressed content (if not already present) and decode encoding content supported in the reply.


Otherwise your code is not following good practices for the Node.js, but without knowing the version you are using (and considering that it is not part of the answer) it is impracticable to suggest changes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.