jQuery to access DOM in a site

前端 未结 1 1248
你的背包
你的背包 2021-01-25 14:43

I am trying to scrape various elements in a table from this site to teach myself scraping using node.js, cheerio and request

I have trouble getting the items in the tabl

相关标签:
1条回答
  • 2021-01-25 15:24

    You're on the right track.

    The $().get() method returns the element. In your case var a is the TR. That's not necessarily what you want.

    What you need to do is further subdivide each row into the individual TD's. I did this using $(this).find('td'). Then, I grab each TD 1 by 1 and extract the text out of it, converting that into an object where the key represents the field of the table. All of these are aggregated into an array, but you can use the basic concept to build whatever data structure you see fit to utilize.

        request('http://www.inc.com/inc5000/index.html', function (error, response, html) {
            if(error || response.statusCode != 200) return;
    
            var $ = cheerio.load(html);
            var DATA = [];
    
            $('tr.ng-scope').each(function(){
                var $tds = $(this).find('td');
    
                DATA.push({
                    rank:     $tds.eq(0).text(),
                    company:  $tds.eq(1).text(),
                    growth:   $tds.eq(2).text(),
                    revenue:  $tds.eq(3).text(),
                    industry: $tds.eq(4).text()
                });
            });
    
            console.log(DATA);
        });
    
    0 讨论(0)
提交回复
热议问题