国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

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

2024-05-06 16:45:07
字體:
供稿:網(wǎng)友

直接開始吧

1.在項目文件夾安裝兩個必須的依賴包

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

npm install cheerio --save-dev

Cheerio

-----cheerio是nodejs的抓取頁面模塊,為服務(wù)器特別定制的,快速、靈活、實施的jQuery核心實現(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)=>{    //獲取頁面文檔數(shù)據(jù)    var content = response.text;    //cheerio也就是nodejs下的jQuery 將整個文檔包裝成一個集合,定義一個變量$接收    var $ = cheerio.load(content);    //定義一個空數(shù)組,用來接收數(shù)據(jù)    var result=[];    //分析文檔結(jié)構(gòu) 先獲取每個li 再遍歷里面的內(nèi)容(此時每個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ù)以對象的形式添加到數(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)前文件夾多出一個boss.json文件(打開boss.json文件,ctrl+A全選之后 ctrl+K,再Ctrl+F即可將json文件自動排版)    fs.writeFile("boss.json",result,"utf-8",(error)=>{      //監(jiān)聽錯誤,如正常輸出,則打印null      if(error==null){        console.log("恭喜您,數(shù)據(jù)爬取成功!請打開json文件,先Ctrl+A,再Ctrl+K,最后Ctrl+F格式化后查看json文件(僅限Visual Studio Code編輯器)");      }    });  });

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 共和县| 营口市| 金寨县| 凉城县| 盐源县| 大姚县| 昌都县| 汉寿县| 张家口市| 牙克石市| 满城县| 湖南省| 什邡市| 石楼县| 东阿县| 巴彦淖尔市| 井研县| 肃宁县| 青海省| 巴塘县| 新乡市| 泸水县| 灵璧县| 榆中县| 冀州市| 湖口县| 都匀市| 澄迈县| 西宁市| 桂平市| 大新县| 明星| 铜川市| 信宜市| 华容县| 赤峰市| 太谷县| 海城市| 旌德县| 伽师县| 华宁县|