Cannot seem to scrape a div class tag in Node.js

2019-07-15 03:23发布

问题:

I'm new to node.js. My experience has been in Java and VBA. I'm trying to scrape a website for a friend and all is going well until I can't get what I’m after.

<div class="gwt-Label ADC2X2-c-q ADC2X2-b-nb ADC2X2-b-Zb">Phone: +4576 102900</div>

That tag just has a text. no attr or anything. Yet I cannot scrape it using cheerio.

if(!err && resp.statusCode == 200){

    var $ = cheerio.load(body);

    var number = $('//tried everything here!').text();
    console.log(number);

this function I also played around with

    $('.ADC2X2').filter(function(i){
        console.log("Sdfs");
        console.log (i);

any suggestions would be greatly appreciated.

thanks all!

回答1:

I take answer from cheerio documentation.

$(".gwt-Label").text();

if that's not working, maybe you have many frame in page. Another possibility is page is renderer at client side, like angular pages, so element your search is not in server html, but only created after page load. If that's true, you will to use a full browser like phantomjs and not only a dom traverser tool like cheerio.