http爬虫

http爬虫

http模块的数据请求: get,post,request
爬虫: 爬去数据,但并不是所有网站都能爬取的,有反爬虫

流程: 后端数据请求 —> 数据分析 —> 数据清洗 ----> 数据前台发送

 const http = require( 'http' )//引入http模块
 const cheerio = require( 'cheerio' )//引入cheerio,用于清洗数据
 
 const options = {//options 就是一个对象,也是一个配置
  hostname: 'jx.1000phone.net',//域名
  port: 80,//端口
  path: '/teacher.php/Class/classDetail/param/rqiWlsefmajGmqJhXXWhl3ZiY2dn',//路径
  method: 'GET',//请求方式
  headers: {//请求头
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Cookie': 'PHPSESSID=ST-91625-drj9QJxH287RYSrtXEIOz7ZePTo-izm5ejd5j1npj2pjc7i3v4z',
    'Host': 'jx.1000phone.net',
    'Pragma': 'no-cache',
    'Referer': 'http://jx.1000phone.net/teacher.php/Class/index',
    'Upgrade-Insecure-Requests': 1,
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': ''
   }
  };
  const req = http.get( options , (res) => { // res   response 响应( 回馈 )

  res.setEncoding('utf8'); // 得到结果的编码

  let rawData = '';
  res.on('data', (chunk) => { rawData += chunk; });
  res.on('end', () => {
    try {
      // console.log( rawData )  // html    string

      const $ = cheerio.load( rawData )

      $('td.student a').each( function ( i, ele) {
        console.log(  $(this).text() )
      })

    } catch (e) {
      console.error(e.message);
    }
  });


}).on('error', (e) => {
  console.error(`Got error: ${e.message}`);
});


req.end()//结束
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值