node怎么爬取數(shù)據(jù)?下面本篇文章給大家分享一個node爬蟲實例,聊聊利用node抓取小說章節(jié)的方法,希望對大家有所幫助!
準備用electron
制作一個小說閱讀工具練練手,那么首先要解決的就是數(shù)據(jù)問題,也就是小說的文本。
這里準備使用nodejs對小說網(wǎng)站進行爬蟲爬取,嘗試爬下一本小說,數(shù)據(jù)就不存放數(shù)據(jù)庫了,先使用txt
作為文本存儲
在node
中對于網(wǎng)站的請求,本身就存在http
和https
庫,內(nèi)部含有request
請求方法。
實例:
request = https.request(TestUrl, { encoding:'utf-8' }, (res)=>{ let chunks = '' res.on('data', (chunk)=>{ chunks += chunk }) res.on('end',function(){ console.log('請求結(jié)束'); }) })
但是也就到此為止了,只是存取了一個html
的文本數(shù)據(jù),并不能夠?qū)?nèi)部元素進行提取之類的工作(也可以正則拿,但是太過復(fù)雜)。
我將訪問到的數(shù)據(jù)通過fs.writeFile
方法存儲起來了,這只是整個網(wǎng)頁的html
但是我想要的還有各個章節(jié)中的內(nèi)容,這樣一來就需要獲取章節(jié)的超鏈接,組成超鏈接鏈表進去爬取
cheerio庫
所以,這里就要介紹一個js的庫了,cheerio
官方文檔:https://cheerio.js.org/
中文文檔:https://github.com/cheeriojs/cheerio/wiki/Chinese-README
在文檔中,可以使用示例進行調(diào)試
使用cheerio解析HTML
cheerio解析html時,獲取dom節(jié)點的方式與jquery
相似。
根據(jù)之前獲取到的書籍首頁的html,查找自己想要的dom節(jié)點數(shù)據(jù)
const fs = require('fs') const cheerio = require('cheerio'); // 引入讀取方法 const { getFile, writeFun } = require('./requestNovel') let hasIndexPromise = getFile('./hasGetfile/index.html'); let bookArray = []; hasIndexPromise.then((res)=>{ let htmlstr = res; let $ = cheerio.load(htmlstr); $(".listmain dl dd a").map((index, item)=>{ let name = $(item).text(), href = 'https://www.shuquge.com/txt/147032/' + $(item).attr('href') if (index > 11){ bookArray.push({ name, href }) } }) // console.log(bookArray) writeFun('./hasGetfile/hrefList.txt', JSON.stringify(bookArray), 'w') })
打印一下信息
可以同時將這些信息也存儲起來
現(xiàn)在章節(jié)數(shù)和章節(jié)的鏈接都有了,那么就可以獲取章節(jié)的內(nèi)容了。
因為批量爬取最后需要IP代理,這里還沒準備,暫時先寫獲取某一章節(jié)小說的內(nèi)容方法
爬取某一章節(jié)的內(nèi)容其實也比較簡單:
// 爬取某一章節(jié)的內(nèi)容方法 function getOneChapter(n) { return new Promise((resolve, reject)=>{ if (n >= bookArray.length) { reject('未能找到') } let name = bookArray[n].name; request = https.request(bookArray[n].href, { encoding:'gbk' }, (res)=>{ let html = '' res.on('data', chunk=>{ html += chunk; }) res.on('end', ()=>{ let $ = cheerio.load(html); let content = $("#content").text(); if (content) { // 寫成txt writeFun(`./hasGetfile/${name}.txt`, content, 'w') resolve(content); } else { reject('未能找到') } }) }) request.end(); }) } getOneChapter(10)
這樣,就可以根據(jù)上面的方法,來創(chuàng)造一個調(diào)用接口,傳入不同的章節(jié)參數(shù),獲取當前章節(jié)的數(shù)據(jù)
const express = require('express'); const IO = express(); const { getAllChapter, getOneChapter } = require('./readIndex') // 獲取章節(jié)超鏈接鏈表 getAllChapter(); IO.use('/book',function(req, res) { // 參數(shù) let query = req.query; if (query.n) { // 獲取某一章節(jié)數(shù)據(jù) let promise = getOneChapter(parseInt(query.n - 1)); promise.then((d)=>{ res.json({ d: d }) }, (d)=>{ res.json({ d: d }) }) } else { res.json({ d: 404 }) } }) //服務(wù)器本地主機的數(shù)字 IO.listen('7001',function(){ console.log("啟動了。。。"); })
效果:
現(xiàn)在,一個簡單的查找章節(jié)接口就做好了,并且也可以做一些參數(shù)超出判斷。
對于不同的數(shù)據(jù)接口,爬蟲處理方式也不一樣,不過在本次爬取的鏈接中,內(nèi)容的顯示并不是由前端動態(tài)渲染出來的,所以可以直接爬取靜態(tài)的html即可。如果遇到數(shù)據(jù)是通過Ajax之類的方式獲取到的json串,那就要通過網(wǎng)絡(luò)接口去請求數(shù)據(jù)了。