nodejs制作小爬蟲功能示例
本文實(shí)例講述了nodejs制作小爬蟲功能。分享給大家供大家參考,具體如下:
1 安裝nodejs
2 安裝需要模塊
npm install request cheerio
3 新建js文件
4 引入
const request=require("request") const cheerio=require("cheerio")
5 利用request模塊發(fā)送請求
request('http://news.dgut.edu.cn/dgut/xydt/news_list.shtml',function(err,res){ if(err) { console.log('請求出錯'); } else { var $ = cheerio.load(res.body, {decodeEntities: false}); $('.listList').children('ul').children('li').each(function(){ //找到li元素對象然后通過each遍歷 var newsTitle = $(this).children('a').text(); //得到<a>標(biāo)簽的文字 var newsTime= $(this).children('span').eq(1).text();//得到第二個<span>標(biāo)簽的文字 var newsUrl= "http://news.dgut.edu.cn"+$(this).children('a').attr('href');//得到<a>標(biāo)簽的href的值 item++; console.log("已爬取"+item+"條記錄"); }); } });
一個小爬蟲案例就完了
附上完整代碼
request('http://news.dgut.edu.cn/dgut/xydt/news_list.shtml',function(err,res){ if(err) { console.log('請求出錯'); } else { var $ = cheerio.load(res.body, {decodeEntities: false}); $('.listList').children('ul').children('li').each(function(){ //找到li元素對象然后通過each遍歷 var newsTitle = $(this).children('a').text(); //得到<a>標(biāo)簽的文字 var newsTime= $(this).children('span').eq(1).text();//得到第二個<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ù)庫
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('請求出錯'); } else { var $ = cheerio.load(res.body, {decodeEntities: false}); $('.listList').children('ul').children('li').each(function(){ //找到li元素對象然后通過each遍歷 var newsTitle = $(this).children('a').text(); //得到<a>標(biāo)簽的文字 var newsTime= $(this).children('span').eq(1).text();//得到第二個<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ù)庫連接錯誤"); } }) item++; console.log("已爬取"+item+"條記錄"); }); } });
希望本文所述對大家node.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
通過NodeJS輕松使用GRPC和協(xié)議緩沖區(qū)的方法
本文介紹了GRPC和協(xié)議緩沖區(qū)的基本概念,并展示了如何在NodeJS應(yīng)用程序中使用它們,GRPC是一個高性能RPC框架,協(xié)議緩沖區(qū)則用于定義服務(wù)和序列化消息,本文給大家介紹如何在NodeJS應(yīng)用程序中使用GRPC和協(xié)議緩沖區(qū),感興趣的朋友一起看看吧2024-10-10使用Node.js實(shí)現(xiàn)ORM的一種思路詳解(圖文)
這篇文章主要介紹了用Node.js實(shí)現(xiàn)ORM的一種思路詳解(圖文),需要的朋友可以參考下2017-10-10Nodejs+angularjs結(jié)合multiparty實(shí)現(xiàn)多圖片上傳的示例代碼
這篇文章主要介紹了Nodejs+angularjs結(jié)合multiparty實(shí)現(xiàn)多圖片上傳的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09node.js 基于 STMP 協(xié)議和 EWS 協(xié)議發(fā)送郵件
這篇文章主要介紹了node.js 基于 STMP 協(xié)議和 EWS 協(xié)議發(fā)送郵件的示例,幫助大家更好的理解和使用node.js,感興趣的朋友可以了解下2021-02-02淺析node應(yīng)用的timing-attack安全漏洞
本篇文章給大家通過原理的原因分析了node應(yīng)用的timing-attack安全漏洞問題,有興趣的朋友閱讀參考下。2018-02-02