開始之前請先確保自己安裝了Node.js環(huán)境,還沒有安裝的的童鞋請看一下安裝教程......
專業(yè)領(lǐng)域包括成都網(wǎng)站設(shè)計、網(wǎng)站制作、商城系統(tǒng)網(wǎng)站開發(fā)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,成都創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。
https://www.jb51.net/article/113677.htm
https://www.jb51.net/article/57687.htm
直接開始吧
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 是一個輕量的,漸進式的ajax api,可讀性好,學習曲線低,內(nèi)部依賴nodejs原生的請求api,適用于nodejs環(huán)境下
npm install cheerio --save-dev
Cheerio
-----cheerio是nodejs的抓取頁面模塊,為服務(wù)器特別定制的,快速、靈活、實施的jQuery核心實現(xiàn)。適合各種Web爬蟲程序。相當于node.js中的jQuery
2.新建 crawler.js 文件
//導入依賴包 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文件里 刷新目錄 即可看到當前文件夾多出一個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)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章名稱:手把手教你用Node.js爬蟲爬取網(wǎng)站數(shù)據(jù)的方法
本文鏈接:http://bm7419.com/article18/goeegp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、手機網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站、電子商務(wù)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)