使用node写爬虫

node爬虫

爬虫介绍

  1. 爬取接口

    • 使用 axios
    • 使用与接口类型的爬取
  2. 爬取页面

    • 使用 request + cheerio

    • 适用于后端渲染,直接返回 HTML 页面的情况

      • cheerio 使用方法类似于 jQuery

      • 文档

      • 关于request 转码问题

        const cheerio = require('cheerio')
        const requuest = require('request-promise')
        
        // 转码问题
        let iconv = require('iconv-lite')
        
        let url = 'http://top.baidu.com/category?c=10&fr=topindex'
        let options = {
          url,
          encoding: null // 告诉 request 不要帮我把 buffer 转成字符串
        }
        
        request(options, async (err, response, body) => {
          // console.log(body.toString())	// 默认转 utf8 编码
        
          // 获取返回的编码格式
          let ContentType = response.headers['content-type']
          let encoding;
          if (ContentType.lastIndexOf('=') > 0) {
            encoding = ContentType.slice(ContentType.lastIndexOf('=') + 1) || 'utf8'
          } else {
            let b = body.toString()
            let res = b.match(/charset=(.+?)"/)
            if (res) {
              encoding = res[1]
            }
          }
          if (!encoding) {
            encoding = 'utf8'
          }
        
          body = iconv.decode(body, encoding)
          let $ = cheerio.load(body)
          let result = []
          $('a.list-title').each((index, item) => {
            let $this = $(item)
            result.push($this.text())
          })
          console.log(result)
        })
        
   >  DEMO

   ```javascript
   const cheerio = require('cheerio')
   const requuest = require('request')
   
   let url = 'https://juejin.im/timeline'
   
   request(url, async (err, responst, body) => {
     // console.log(body);
     let $ = await cheerio.load(body)
     let Arrays = []
     let actions = $(".sale")
   
     actions.each((index, item) => {
       Arrays.push({
         text: actions.text(),
       })
     })
     console.log(Arrays);
   })
   
   /**
   [ 
   	{ text: '最高可省 15 元最高可省 15 元最高可省 15 元' },
     	{ text: '最高可省 15 元最高可省 15 元最高可省 15 元' },
     	{ text: '最高可省 15 元最高可省 15 元最高可省 15 元' } 
    ] 
    **/



​ ```

  1. 前后端分离方式渲染的页面 例如 vue react
爬虫步骤
  1. 发起 HTTP 请求获取网页内容
  2. 使用类似 Jquery 的语法来操作网页提取需要的数据
  3. 吧数据保存到数据库中以供查询
  4. 建立一个服务器来显示这些数据
  5. 可以定时爬取数据
  6. 让程序稳定运行
  7. 对编码进行转换
核心类库
  • request

发送邮件

nodemailer 一个简易的 Node.js 邮件发送模块

解决乱码问题

  1. 检查 content-type 编码格式

    
    
  2. 检查 是否使用了 代码压缩问题 content-encoding

    解决代码压缩问题

    how to unzip gzip response in request

  3. 请求返回 504 Bad Gateway 目标网站阻止了这类的访问,只要在请求中加上伪装成浏览器的header就可以了

    // 请求头中增加伪装信息
    headers = {  
        'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  
    }  
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值