用Node.JS刮?

时间:2017-03-09 17:30:05

标签: javascript node.js web-scraping cheerio

我想从带有Node.JS的网页上抓头,但我无法弄清楚如何。感谢cheerio,我可以访问所有身体:

request(webUrl, function(err, resp, body){
    if(!err && resp.statusCode == 200) {
        var $ = cheerio.load(body);

        //Getting all the links 'a' from the webpage
        $('a').each(function(){

            //Getting the href attribute from the 'a' link
            var url = $(this).attr('href');

            //We keep the link only if it is the same root (in order to avoid the 'undefined' links and the subdomains or outside links (like social media links))
            if(url != undefined && url[0] == '/') {

                //We add the domain name to the url we got in order to have the full 
                url = websiteUrl + url;
                urls.push(url);
            }

        });
        console.log(urls);
    }
});

但不可能用这种方法获得头脑。我试过这个,但它只给了我身体脚本,而不是标题中的那些:

request(webUrl, function(err, resp, body){
    if(!err && resp.statusCode == 200) {
        var $ = cheerio.load(body);

        $('script').each(function(){

            //Getting the href attribute from the 'a' link
            var url = $(this).attr('src');
            console.log(url);

            if(url != undefined) {


                wowo.push(url);
            }

        });
        console.log(wowo);
    }
});

有人可以帮我吗? :'(