nodejs制作小爬蟲(chóng)功能示例
更新時(shí)間:2020年02月24日 10:44:11 作者:巴啦啦小能量
這篇文章主要介紹了nodejs制作小爬蟲(chóng)功能,結(jié)合實(shí)例形式分析了node.js安裝request、cheerio模塊及請(qǐng)求發(fā)送、數(shù)據(jù)庫(kù)操作等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
本文實(shí)例講述了nodejs制作小爬蟲(chóng)功能。分享給大家供大家參考,具體如下:
1 安裝nodejs
2 安裝需要模塊
npm install request cheerio
3 新建js文件
4 引入
const request=require("request")
const cheerio=require("cheerio")
5 利用request模塊發(fā)送請(qǐng)求
request('http://news.dgut.edu.cn/dgut/xydt/news_list.shtml',function(err,res){
if(err)
{
console.log('請(qǐng)求出錯(cuò)');
}
else
{
var $ = cheerio.load(res.body, {decodeEntities: false});
$('.listList').children('ul').children('li').each(function(){ //找到li元素對(duì)象然后通過(guò)each遍歷
var newsTitle = $(this).children('a').text(); //得到<a>標(biāo)簽的文字
var newsTime= $(this).children('span').eq(1).text();//得到第二個(gè)<span>標(biāo)簽的文字
var newsUrl= "http://news.dgut.edu.cn"+$(this).children('a').attr('href');//得到<a>標(biāo)簽的href的值
item++;
console.log("已爬取"+item+"條記錄");
});
}
});
一個(gè)小爬蟲(chóng)案例就完了
附上完整代碼
request('http://news.dgut.edu.cn/dgut/xydt/news_list.shtml',function(err,res){
if(err)
{
console.log('請(qǐng)求出錯(cuò)');
}
else
{
var $ = cheerio.load(res.body, {decodeEntities: false});
$('.listList').children('ul').children('li').each(function(){ //找到li元素對(duì)象然后通過(guò)each遍歷
var newsTitle = $(this).children('a').text(); //得到<a>標(biāo)簽的文字
var newsTime= $(this).children('span').eq(1).text();//得到第二個(gè)<span>標(biāo)簽的文字
var newsUrl= "http://news.dgut.edu.cn"+$(this).children('a').attr('href');//得到<a>標(biāo)簽的href的值
item++;
console.log("已爬取"+item+"條記錄");
});
}
});
下面的帶數(shù)據(jù)庫(kù)
const request=require("request")
const cheerio=require("cheerio")
const mysql=require('mysql')
const db=mysql.createPool({host:'120.79.5554',user:'root',password:'root',database:'pachong'});
var item=0;
request('http://news.dgut.edu.cn/dgut/xydt/news_list.shtml',function(err,res){
if(err)
{
console.log('請(qǐng)求出錯(cuò)');
}
else
{
var $ = cheerio.load(res.body, {decodeEntities: false});
$('.listList').children('ul').children('li').each(function(){ //找到li元素對(duì)象然后通過(guò)each遍歷
var newsTitle = $(this).children('a').text(); //得到<a>標(biāo)簽的文字
var newsTime= $(this).children('span').eq(1).text();//得到第二個(gè)<span>標(biāo)簽的文字
var newsUrl= "http://news.dgut.edu.cn"+$(this).children('a').attr('href');//得到<a>標(biāo)簽的href的值
console.log(newsTitle,newsTime,newsUrl)
db.query(`INSERT INTO news (newsTitle, newsTime, newsUrl) VALUE('${newsTitle}', '${newsTime}','${newsUrl}')`,function(err,data){
if(err)
{
console.log("數(shù)據(jù)庫(kù)連接錯(cuò)誤");
}
})
item++;
console.log("已爬取"+item+"條記錄");
});
}
});
希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
在Node.js中發(fā)出HTTP請(qǐng)求的 5 種方法
學(xué)習(xí)如何在 Node.js 中發(fā)出 HTTP 請(qǐng)求可能會(huì)讓人感到不知所措,因?yàn)橛袛?shù)十個(gè)可用的庫(kù),每個(gè)解決方案都聲稱比上一個(gè)更高效,在這篇文章中,我們將探討在 Node.js 中發(fā)出 HTTP 請(qǐng)求的五種最流行的方法,并為每種方法提供說(shuō)明,需要的朋友可以參考下2023-11-11
利用nodejs監(jiān)控文件變化并使用sftp上傳到服務(wù)器
這篇文章主要介紹了利用nodejs監(jiān)控文件變化并使用sftp上傳到服務(wù)器的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
node?NPM庫(kù)增強(qiáng)版globby?Promise使用學(xué)習(xí)
這篇文章主要為大家介紹了node?NPM庫(kù)增強(qiáng)版globby?Promise使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
使用Express處理請(qǐng)求和托管靜態(tài)資源方式
這篇文章主要介紹了使用Express處理請(qǐng)求和托管靜態(tài)資源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

