Socket
Socket
Sign inDemoInstall

ease-crawler

Package Overview
Dependencies
131
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ease-crawler

基于puppeteer的爬虫


Version published
Weekly downloads
18
increased by5.88%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

基于puppeteer的易用爬虫

常用需求

  • 页面数据抓取
  • 截屏保存图片,并上传至nos
  • 保存数据到csv

后续功能待添加...

数据抓取demo

  • 用puppeteer打开页面
  • 在页面上执行一段js代码,操作dom选取你要抓取的数据
const easeCrawler = require('ease-crawler')

const crawler = new easeCrawler({
  disable: false,
  args: {
    headless: false,
    ignoreHTTPSErrors: true
  }
})

// 由于使用了puppeteer-pool,需要初始化完成之后才能进行操作
crawler.init().then(() => {
  crawler.crawl('https://example.com', async (browser, curPage) => {
    let content = await curPage.evaluate(() => {
      return $('div').html()
    })
    // content你所选取的节点内容
  }, (err) => { console.log('err', err) })
})

创建实例参数

参数名类型描述默认值
disableboolean是否禁用css和imgfalse
argsobjectpuppeteer launch时传入的参数{}

更多抓取用法参考puppeteer的api

API

crawl(url, onSuccess, onError)

用于抓取页面数据

  • url(string):要抓取页面的url地址
  • onSuccess(function):puppeteer打开页面成功的回调,会把browser,curPage传入
  • onError(function):puppeteer打开页面出错的回调,会把err传入

waitForAjax(page)

有些页面需要等待ajax请求完成之后才能抓取数据

  • 需要传入page实例

disableImgAndCss(page)

禁用页面的css和img

  • 需要传入page实例

detectJquery(page)

检测页面是否有jq,没有则注入

  • 需要传入page实例
  • return promise

timeChunk(arr, callBack, count, interval, ...other)

分时函数,用于防止抓取速度过快被封ip

  • arr(Array):需要分时的数组
  • callBack(function):分时执行的函数
  • count(number):一定时间内执行的次数
  • interval(number):执行间隔的秒数
  • return promise

saveToCsv(header, content, name)

用于把数据保存至csv

  • header(Array):定义表头
  • content(object):内容,key需要和表头的值对应
  • name(string):文件名
crawler.saveToCsv(['city', 'name'], {city: '杭州', name: '最强'}, 'book')

调用此函数之后,会在项目的根目录创建一个csv文件,使用gbk编码,如果是window可能会乱码

用sublime打开csv,file => save with encoding => UTF-8 with BOM 可以解决此问题

screenShot({url, width, height, onSuccess, onError})

用于截屏,提供的url必须可以在浏览器中打开

  1. 使用puppeteer打开页面
  2. 截屏并把图片上传到nos
  • url(stirng):所需截屏的页面
  • width(number):截屏的宽度(移动端使用屏幕宽度即可),默认值为375
  • height(number):截屏的高度,默认值为667
  • onSuccess(function):截屏成功的回调,会传入图片的url地址
  • onError(function):截屏错误的回调,会传入err
 crawler.screenShot({
      url: xxx,
      width: xxx,
      height: xxx,
      onSuccess: (url) => {
        console.log('url')
      },
      onError: (err) => {
        console.log(`server occur error: ${err}`)
      }
    })

Keywords

FAQs

Last updated on 18 Dec 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc