npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ease-crawler

v1.1.7

Published

基于puppeteer的爬虫

Downloads

3

Readme

基于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) })
})

创建实例参数

参数名 | 类型 | 描述 | 默认值 ---|---|---|--- disable | boolean | 是否禁用css和img | false args | object | puppeteer 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}`)
      }
    })