node.js 爬蟲

pc.js 代碼

var http=require ("http");
var url="http://sports.sina.com.cn/nba/1.shtml";
 http.get(url,(res)=>{

     var html="";
     res.on("data",function (chunk) {
         html+=chunk;
     });
     res.on("end",function () {
         console.log(html);
     });
 //    后臺(tái)返回的數(shù)據(jù),攜帶chunk數(shù)據(jù)
 }).on("error",(e)=>{
     console.log(e.message);
 //    如果在訪問過程中有錯(cuò)誤,輸出錯(cuò)誤信息
 });

爬取 新浪nba球明星
運(yùn)行:
node pc.js
只能爬出來 網(wǎng)頁的源代碼
此處需要一個(gè)npm 庫
cheerio

這是一個(gè)用正則來篩選信息的庫

npm install cheerio
pc1.js 代碼

var http=require ("http");
var cheerio=require("cheerio");
var url="http://sports.sina.com.cn/nba/1.shtml";
 http.get(url,(res)=>{

     var html="";
     res.on("data",function (chunk) {
         html+=chunk;
     });
     res.on("end",function () {
         // console.log(html);
         var $=cheerio.load(html);
         console.log($("#right a").html());
         $("#right a").each(function () {
             console.log($(this).attr("href"));
         });
     });
 //    后臺(tái)返回的數(shù)據(jù),攜帶chunk數(shù)據(jù)
 }).on("error",(e)=>{
     console.log(e.message);
 //    如果在訪問過程中有錯(cuò)誤,輸出錯(cuò)誤信息
 });

運(yùn)行:
node pc.js

能獲取到網(wǎng)頁的href標(biāo)簽內(nèi)容

pc2.js

var http=require ("http");
var cheerio=require("cheerio");
var fs=require("fs");
var url="http://sports.sina.com.cn/nba/1.shtml";

function httpGet(url,cb) {
    var html="";
    http.get(url,function (res) {
        res.on("data",function (chunk) {
            html+=chunk;
        });
        res.on("end",function () {
            cb(html);
        })
    }).on("error",function (e) {
        console.log(e.message);
    });
    return html;
}
httpGet(url,function (html) {
    var $=cheerio.load(html);
    $("#right a").each(function (index) {
        var newUrl=$(this).attr("href");
        httpGet(newUrl,function (body) {
            var jq=cheerio.load(body);
            fs.writeFile(`./news/${index}.txt`,jq("#artibody").text(),function (err) {
                //用node.js 把獲取到的text放入一個(gè)news文件夾
                if(err){
                    return console.log(err.message);
                }
                console.log("完成");
            })
        })

    })

});

運(yùn)行:
node pc.js

一個(gè)封裝好的httpGet函數(shù) 并且用 node.js 里邊的 fs.writeFile函數(shù) 將獲取到的數(shù)據(jù) 放在一個(gè)new的文件夾中

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 你可能會(huì)把 NodeJS 用作網(wǎng)絡(luò)服務(wù)器,但你知道它還可以用來做爬蟲嗎? 本教程中會(huì)介紹如何爬取靜態(tài)網(wǎng)頁——還有那...
    張嘉夫閱讀 5,124評(píng)論 3 51
  • 前言:最近想學(xué)習(xí)node.js,突然在網(wǎng)上看到基于node的爬蟲制作教程,所以簡(jiǎn)單學(xué)習(xí)了一下,把這篇文章分享給同樣...
    京東內(nèi)部?jī)?yōu)惠券閱讀 1,401評(píng)論 0 12
  • 一、準(zhǔn)備階段 當(dāng)我們需要使用Node.js進(jìn)行爬蟲爬取網(wǎng)頁時(shí),我們通常需要下載兩個(gè)庫request和cheerio...
    Srtian閱讀 186評(píng)論 0 0
  • 一、項(xiàng)目描述 引言:在電影天堂下電視劇的下伙伴有木有發(fā)現(xiàn),它沒有提供批量下載功能,美劇英劇還好,10集左右,我就多...
    danieldai閱讀 5,350評(píng)論 8 19
  • 我的2017—— 那些悲傷和思念難以告別 那些奇葩的事情啼笑皆非 那些老去的時(shí)光無力的呻吟 那些稚嫩的笑聲是上天的...
    薊門閑客閱讀 131評(píng)論 0 0