手把手教你用Node.js爬蟲爬取網(wǎng)站數(shù)據(jù)的方法
開始之前請先確保自己安裝了Node.js環(huán)境,還沒有安裝的的童鞋請看一下安裝教程......
http://www.dhdzp.com/article/113677.htm
http://www.dhdzp.com/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,可讀性好,學習曲線低,內部依賴nodejs原生的請求api,適用于nodejs環(huán)境下
npm install cheerio --save-dev
Cheerio
-----cheerio是nodejs的抓取頁面模塊,為服務器特別定制的,快速、靈活、實施的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=[];
//分析文檔結構 先獲取每個li 再遍歷里面的內容(此時每個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ù)組轉換成字符串
result=JSON.stringify(result);
//將數(shù)組輸出到json文件里 刷新目錄 即可看到當前文件夾多出一個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編輯器)");
}
});
});
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
基于node.js依賴express解析post請求四種數(shù)據(jù)格式
本篇文章主要介紹了node.js依賴express解析post請求四種數(shù)據(jù)格式,主要是www-form-urlencoded,form-data,application/json,text/xml,有興趣的可以了解一下。2017-02-02
node koa2實現(xiàn)上傳圖片并且同步上傳到七牛云存儲
這篇文章主要介紹了node koa2實現(xiàn)上傳圖片并且同步上傳到七牛云存儲,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Node使用koa2實現(xiàn)一個簡單JWT鑒權的方法
這篇文章主要介紹了Node使用koa2實現(xiàn)一個簡單JWT鑒權的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
node.js中的http.response.write方法使用說明
這篇文章主要介紹了node.js中的http.response.write方法使用說明,本文介紹了http.response.write的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下2014-12-12
nodejs body-parser 解析post數(shù)據(jù)實例
下面小編就為大家?guī)硪黄猲odejs body-parser 解析post數(shù)據(jù)實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

