用node写爬虫

查看node官方文档,根据不同url类型对应发起相应的网络请求
在这里插入图片描述

1、发送一个Http请求

在这里插入图片描述

const http  = require("http");//引入http模块
//创建请求对象发送请求(未发送http请求)
let req=http.request('http://web.itheima.com/teacher.html',res=>{
  //  console.log(res);
  let chunks=[];
  //监听data事件,获取传递过来的数据片段
  //拼接数据片段
  res.on('data',c=>{
      chunks.push(c);
  })
  //监听end事件,获取数据完毕时触发
  res.on('end',()=>{
      //拼接所有的chunks,并转换成字符串==>html字符串
     console.log( Buffer.concat(chunks).toString('utf-8'));
  })
})
req.end();//将请求发出去

在这里插入图片描述

2、发送一个Https请求

const https  = require("https");//引入https模块
//创建请求对象发送请求(未发送http请求)
const options = {
    hostname: 'www.runoob.com',
    port: 443,
    path: '/',
    method: 'GET'
  };
let req=https.request(options,res=>{
  //  console.log(res);
  let chunks=[];
  //监听data事件,获取传递过来的数据片段
  //拼接数据片段
  res.on('data',c=>{
      chunks.push(c);
  })
  //监听end事件,获取数据完毕时触发
  res.on('end',()=>{
      //拼接所有的chunks,并转换成字符串==>html字符串
     console.log( Buffer.concat(chunks).toString('utf-8'));
  })
})
req.end();//将请求发出去

在这里插入图片描述

3、通过cheerio库对页面内容进行分析

cheerio的实现是基于Jquery的核心库,并且可以直接使用jq一样的api

const cheerio  = require("cheerio");
const http  = require("http");//引入http模块
const download  = require("download");
//创建请求对象发送请求(未发送http请求)
const host="http://web.itheima.com/";
let req=http.request('http://web.itheima.com/teacher.html',res=>{
  //  console.log(res);
  let chunks=[];
  //监听data事件,获取传递过来的数据片段
  //拼接数据片段
  res.on('data',c=>{
      chunks.push(c);
  })
  //监听end事件,获取数据完毕时触发
  res.on('end',()=>{
      //拼接所有的chunks,并转换成字符串==>html字符串
      //console.log( Buffer.concat(chunks).toString('utf-8'));
    let htmlStr=Buffer.concat(chunks).toString('utf-8');
    let $=cheerio.load(htmlStr);
    let imgs=[];
    console.log($('.tea_main .tea_con .tea_txt > ul > li > img').attr('src'));
    console.log($('.tea_main .tea_con .tea_txt > ul > li > img').length);
    $('.tea_main .tea_con .tea_txt > ul > li > img').each((index,item)=>{
        console.log(host+$(item).attr('src'));
        //如果下载文件名有中文,一定要用encodeuri进行base64编码
        imgs.push(encodeURI(host+$(item).attr('src')));
    })
    console.log(imgs);
    })
})
req.end();//将请求发出去

在这里插入图片描述

4、通过download库对图片进行下载

const cheerio  = require("cheerio");
const http  = require("http");//引入http模块
const download  = require("download");
//创建请求对象发送请求(未发送http请求)
const host="http://web.itheima.com/";
let req=http.request('http://web.itheima.com/teacher.html',res=>{
  //  console.log(res);
  let chunks=[];
  //监听data事件,获取传递过来的数据片段
  //拼接数据片段
  res.on('data',c=>{
      chunks.push(c);
  })
  //监听end事件,获取数据完毕时触发
  res.on('end',()=>{
      //拼接所有的chunks,并转换成字符串==>html字符串
      //console.log( Buffer.concat(chunks).toString('utf-8'));
    let htmlStr=Buffer.concat(chunks).toString('utf-8');
    let $=cheerio.load(htmlStr);
    let imgs=[];
    $('.tea_main .tea_con .tea_txt > ul > li > img').each((index,item)=>{
        imgs.push(encodeURI(host+$(item).attr('src')));
    })
    Promise.all(imgs.map(x=>download(x,'dist'))).then(()=>{
        console.log('files download');
    })
    })
})
req.end();//将请求发出去

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值