溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

手把手教你用Node.js爬蟲爬取網(wǎng)站數(shù)據(jù)的方法

發(fā)布時(shí)間:2020-10-02 04:52:59 來(lái)源:腳本之家 閱讀:216 作者:WebCandy 欄目:web開(kāi)發(fā)

開(kāi)始之前請(qǐng)先確保自己安裝了Node.js環(huán)境,還沒(méi)有安裝的的童鞋請(qǐng)看一下安裝教程......

https://www.jb51.net/article/113677.htm

https://www.jb51.net/article/57687.htm

直接開(kāi)始吧

1.在項(xiàng)目文件夾安裝兩個(gè)必須的依賴包

npm install superagent --save-dev

SuperAgent(官網(wǎng)是這樣解釋的)

-----SuperAgent is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing request APIs. It also works with Node.js!

-----superagent 是一個(gè)輕量的,漸進(jìn)式的ajax api,可讀性好,學(xué)習(xí)曲線低,內(nèi)部依賴nodejs原生的請(qǐng)求api,適用于nodejs環(huán)境下

npm install cheerio --save-dev

Cheerio

-----cheerio是nodejs的抓取頁(yè)面模塊,為服務(wù)器特別定制的,快速、靈活、實(shí)施的jQuery核心實(shí)現(xiàn)。適合各種Web爬蟲程序。相當(dāng)于node.js中的jQuery

2.新建  crawler.js  文件

//導(dǎo)入依賴包
const http    = require("http");
const path    = require("path");
const url    = require("url");
const fs     = require("fs");
 
const superagent = require("superagent");
const cheerio  = require("cheerio");
3.看注釋啦(這里爬取的是boss直聘網(wǎng)站的數(shù)據(jù))
superagent
  .get("https://www.zhipin.com/job_detail/?city=100010000&source=10&query=%E5%89%8D%E7%AB%AF")
  .end((error,response)=>{
    //獲取頁(yè)面文檔數(shù)據(jù)
    var content = response.text;
    //cheerio也就是nodejs下的jQuery 將整個(gè)文檔包裝成一個(gè)集合,定義一個(gè)變量$接收
    var $ = cheerio.load(content);
    //定義一個(gè)空數(shù)組,用來(lái)接收數(shù)據(jù)
    var result=[];
    //分析文檔結(jié)構(gòu) 先獲取每個(gè)li 再遍歷里面的內(nèi)容(此時(shí)每個(gè)li里面就存放著我們想要獲取的數(shù)據(jù))
    $(".job-list li .job-primary").each((index,value)=>{
      //地址和類型為一行顯示,需要用到字符串截取
      //地址
      let address=$(value).find(".info-primary").children().eq(1).html();
      //類型
      let type=$(value).find(".info-company p").html();
      //解碼
      address=unescape(address.replace(/&#x/g,'%u').replace(/;/g,''));
      type=unescape(type.replace(/&#x/g,'%u').replace(/;/g,''))
      //字符串截取
      let addressArr=address.split('<em class="vline"></em>');
      let typeArr=type.split('<em class="vline"></em>');
      //將獲取的數(shù)據(jù)以對(duì)象的形式添加到數(shù)組中
      result.push({
        title:$(value).find(".name .job-title").text(),
        money:$(value).find(".name .red").text(),
        address:addressArr,
        company:$(value).find(".info-company a").text(),
        type:typeArr,
        position:$(value).find(".info-publis .name").text(),
        txImg:$(value).find(".info-publis img").attr("src"),
        time:$(value).find(".info-publis p").text()
      });
      // console.log(typeof $(value).find(".info-primary").children().eq(1).html());
    });
    //將數(shù)組轉(zhuǎn)換成字符串
    result=JSON.stringify(result);
    //將數(shù)組輸出到j(luò)son文件里 刷新目錄 即可看到當(dāng)前文件夾多出一個(gè)boss.json文件(打開(kāi)boss.json文件,ctrl+A全選之后 ctrl+K,再Ctrl+F即可將json文件自動(dòng)排版)
    fs.writeFile("boss.json",result,"utf-8",(error)=>{
      //監(jiān)聽(tīng)錯(cuò)誤,如正常輸出,則打印null
      if(error==null){
        console.log("恭喜您,數(shù)據(jù)爬取成功!請(qǐng)打開(kāi)json文件,先Ctrl+A,再Ctrl+K,最后Ctrl+F格式化后查看json文件(僅限Visual Studio Code編輯器)");
      }
    });
  });

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI