nodejs制作小爬虫功能示例

所属分类: 网络编程 / JavaScript 阅读数: 1022
收藏 0 赞 0 分享

本文实例讲述了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程序设计有所帮助。

更多精彩内容其他人还在看

jquery实现可拖拽弹出层特效

这篇文章主要介绍了jquery实现可拖拽弹出层特效,代码非常精简,效果非常棒,这里推荐给有需要的小伙伴
收藏 0 赞 0 分享

浅谈 javascript 事件处理

本文向大家简单介绍了javascript的事件处理机制,从事件源,事件操作到事件处理程序都做了简单介绍,并给出了部分示例,这里推荐给大家。
收藏 0 赞 0 分享

jQuery中:reset选择器用法实例

这篇文章主要介绍了jQuery中:reset选择器用法,实例分析了:reset选择器的功能、定义及匹配重置按钮的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

原生javascript实现隔行换色

这篇文章主要介绍了原生javascript实现隔行换色,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:button选择器用法实例

这篇文章主要介绍了jQuery中:button选择器用法,实例分析了:button选择器的功能、定义及选取按钮的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:file选择器用法实例

这篇文章主要介绍了jQuery中:file选择器用法,实例分析了:file选择器的功能、定义及选取类型为file的<input>元素的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:enabled选择器用法实例

这篇文章主要介绍了jQuery中:enabled选择器用法,实例分析了:enabled选择器的功能、定义及选取所有可用元素的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

AngularJS中取消对HTML片段转义的方法例子

这篇文章主要介绍了AngularJS中取消对HTML片段转义的方法例子,在一些需要显示HTML的地方,就要取消AngularJS的转义,本文就介绍了这种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:disabled选择器用法实例

这篇文章主要介绍了jQuery中:disabled选择器用法,实例分析了:disabled选择器功能、定义及选取所有禁用的表单元素的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:checked选择器用法实例

这篇文章主要介绍了jQuery中:checked选择器用法,实例分析了:checked选择器的功能、定义及选取选中的复选框或单选按钮时的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多