nodejs制作小爬虫功能示例


Posted in NodeJs onFebruary 24, 2020

本文实例讲述了nodejs制作小爬虫功能。分享给大家供大家参考,具体如下:

1 安装nodejs

2 安装需要模块

npm install request cheerio

3 新建js文件

4 引入

const request=require("request")
const cheerio=require("cheerio")

5 利用request模块发送请求

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>标签的文字
      var newsTime= $(this).children('span').eq(1).text();//得到第二个<span>标签的文字
      var newsUrl= "http://news.dgut.edu.cn"+$(this).children('a').attr('href');//得到<a>标签的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>标签的文字
      var newsTime= $(this).children('span').eq(1).text();//得到第二个<span>标签的文字
      var newsUrl= "http://news.dgut.edu.cn"+$(this).children('a').attr('href');//得到<a>标签的href的值
    item++;
    console.log("已爬取"+item+"条记录");
    });
  }
});

下面的带数据库

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>标签的文字
      var newsTime= $(this).children('span').eq(1).text();//得到第二个<span>标签的文字
      var newsUrl= "http://news.dgut.edu.cn"+$(this).children('a').attr('href');//得到<a>标签的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("数据库连接错误");
      }
    })
    item++;
    console.log("已爬取"+item+"条记录");
    });
  }
});

希望本文所述对大家node.js程序设计有所帮助。

NodeJs 相关文章推荐
nodejs读取memcache示例分享
Jan 02 NodeJs
nodejs npm install全局安装和本地安装的区别
Jun 05 NodeJs
Nodejs实现短信验证码功能
Feb 09 NodeJs
NodeJS设计模式总结【单例模式,适配器模式,装饰模式,观察者模式】
Sep 06 NodeJs
nodejs 图解express+supervisor+ejs的用法(推荐)
Sep 08 NodeJs
Nodejs实现文件上传的示例代码
Sep 26 NodeJs
nodejs基于express实现文件上传的方法
Mar 19 NodeJs
nodejs读取本地中文json文件出现乱码解决方法
Oct 10 NodeJs
nodejs提示:cross-device link not permitted, rename错误的解决方法
Jun 10 NodeJs
nodejs实现百度舆情接口应用示例
Feb 07 NodeJs
Nodejs在局域网配置https访问的实现方法
Oct 17 NodeJs
nodejs利用readline提示输入内容实例代码
Jul 15 NodeJs
nodejs使用socket5进行代理请求的实现
Feb 21 #NodeJs
linux 下以二进制的方式安装 nodejs
Feb 12 #NodeJs
nodejs实现百度舆情接口应用示例
Feb 07 #NodeJs
使用nodeJS中的fs模块对文件及目录进行读写,删除,追加,等操作详解
Feb 06 #NodeJs
nodejs nedb 封装库与使用方法示例
Feb 06 #NodeJs
nodejs实现的http、https 请求封装操作示例
Feb 06 #NodeJs
Nodejs + Websocket 指定发送及群聊的实现
Jan 09 #NodeJs
You might like
我的论坛源代码(六)
2006/10/09 PHP
php 之 没有mysql支持时的替代方案
2006/10/09 PHP
php导入模块文件分享
2015/03/17 PHP
Symfony2使用Doctrine进行数据库查询方法实例总结
2016/03/18 PHP
php生成图片验证码的方法
2016/04/15 PHP
PHP回调函数与匿名函数实例详解
2017/08/16 PHP
laravel 模型查询按照whereIn排序的示例
2019/10/16 PHP
在textarea文本域中显示HTML代码的方法
2007/03/06 Javascript
Javascript document.referrer判断访客来源网址
2020/05/15 Javascript
js获取系统的根路径实现介绍
2013/09/08 Javascript
使用jQuery实现星级评分代码分享
2014/12/09 Javascript
详解JavaScript中shift()方法的使用
2015/06/09 Javascript
jquery.gridrotator实现响应式图片展示画廊效果
2015/06/23 Javascript
在Ubuntu系统上安装Node.JS的教程
2015/10/15 Javascript
JavaScript对象数组的排序处理方法
2015/10/21 Javascript
Jquery中巧用Ajax的beforeSend方法
2016/01/20 Javascript
jQuery+css实现的tab切换标签(兼容各浏览器)
2016/01/28 Javascript
jquery加载单文件vue组件的方法
2017/06/20 jQuery
jQuery创建及操作xml格式数据示例
2018/05/26 jQuery
js动态获取时间的方法分析
2019/08/02 Javascript
jQuery实现本地存储
2020/12/22 jQuery
[03:21]辉夜杯主赛事 12月25日TOP5
2015/12/26 DOTA
python实现异步回调机制代码分享
2014/01/10 Python
python Pandas 读取txt表格的实例
2018/04/29 Python
pytorch 调整某一维度数据顺序的方法
2018/12/08 Python
浅谈Python 多进程默认不能共享全局变量的问题
2019/01/11 Python
python自动发微信监控报警
2019/09/06 Python
Joseph官网:英国小众奢侈品牌
2019/05/17 全球购物
Linux Interview Questions For software testers
2012/06/02 面试题
文明餐桌活动方案
2014/02/11 职场文书
消防应急演练方案
2014/02/12 职场文书
教师新年寄语
2014/04/03 职场文书
《雕塑之美》教学反思
2014/04/24 职场文书
自荐信格式范文
2015/03/04 职场文书
安全责任协议书范本
2016/03/23 职场文书
Mysql 文件配置解析介绍
2022/05/06 MySQL