Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

x-crawl

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

x-crawl

x-crawl is a flexible nodejs crawler library.

  • 3.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
84
decreased by-23.64%
Maintainers
1
Weekly downloads
 
Created
Source

x-crawl

English | 简体中文

x-crawl is a flexible nodejs crawler library. It is used to batch crawl data, network requests and download file resources. Support crawling data asynchronously or synchronously. Since it runs on nodejs, it is friendly to JS/TS developers.

If you feel good, you can support x-crawl repository with a Star.

Features

  • Support asynchronous/synchronous way to crawl data.
  • Support Promise/Callback method to get the result.
  • Anthropomorphic request interval.
  • Crawl pages, JSON, file resources, etc. with simple configuration.
  • Polling function, timing crawling.
  • The built-in puppeteer crawls the page and uses the jsdom library to parse the page.
  • Written in TypeScript, has type hints, and provides generics.

Relationship with puppeteer

The crawlPage API internally uses the puppeteer library to help us crawl pages.

We can do the following:

  • Generate screenshots and PDFs of pages.
  • Crawl a SPA (Single-Page Application) and generate pre-rendered content (i.e. "SSR" (Server-Side Rendering)).
  • Automate form submission, UI testing, keyboard input, etc.

Table of Contents

Install

Take NPM as an example:

npm install x-crawl

Example

Regular crawling: Get the recommended pictures of the youtube homepage every other day as an example:

// 1.Import module ES/CJS
import path from 'node:path'
import xCrawl from 'x-crawl'

// 2.Create a crawler instance
const myXCrawl = xCrawl({
  timeout: 10000, // overtime time
  intervalTime: { max: 3000, min: 2000 } // control request frequency
})

// 3.Set the crawling task
// Call the startPolling API to start the polling function, and the callback function will be called every other day
myXCrawl.startPolling({ d: 1 }, () => {
  // Call crawlPage API to crawl Page
  myXCrawl.crawlPage('https://www.youtube.com/').then((res) => {
    const { browser, jsdom } = res // By default, the JSDOM library is used to parse Page

    // Get the cover image element of the Promoted Video
    const imgEls = jsdom.window.document.querySelectorAll(
      '.yt-core-image--fill-parent-width'
    )

    // set request configuration
    const requestConfig = []
    imgEls.forEach((item) => {
      if (item.src) {
        requestConfig.push(item.src)
      }
    })

    // Call the crawlFile API to crawl pictures
    myXCrawl.crawlFile({
      requestConfig,
      fileConfig: { storeDir: path.resolve(__dirname, './upload') }
    })
      
    // Close the browser
    browser.close()
  })
})

running result:

Note: Do not crawl at will, you can check the robots.txt protocol before crawling. This is just to demonstrate how to use x-crawl.

Core concepts

Create application

An example of a crawler application

Create a new application instance via xCrawl():

import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({
  // options
})

Related options can refer to XCrawlBaseConfig .

Choose crawling mode

A crawler application instance has two crawling modes: asynchronous/synchronous, and each crawler instance can only choose one of them.

import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({
  mode: 'async'
})

The mode option defaults to async .

  • async: asynchronous request, in batch requests, the next request is made without waiting for the current request to complete
  • sync: synchronous request, in batch requests, you need to wait for this request to complete before making the next request

If there is an interval time set, it is necessary to wait for the interval time to end before sending the request.

Multiple crawler application instances
import xCrawl from 'x-crawl'

const myXCrawl1 = xCrawl({
  // options
})

const myXCrawl2 = xCrawl({
  // options
})

Crawl page

Crawl a page via crawlPage()

import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({ 
  timeout: 10000
})

myXCrawl.crawlPage('https://xxx.com').then(res => {
  const { jsdom, browser, page } = res
  
  // Close the browser
  browser.close()
})
jsdom

Refer to jsdom for specific usage.

browser

**Purpose of calling close: **browser will keep running, so the file will not be terminated. Do not call crawlPage or page if you need to use it later. When you modify the properties of the browser object, it will affect the browser inside the crawlPage of the crawler instance, the returned page, and the browser, because the browser is shared within the crawlPage API of the crawler instance.

Refer to browser for specific usage.

page

The page attribute can be used for interactive operations such as events. For details, refer to page.

Crawl interface

Crawl interface data through crawlData()

import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({ 
  timeout: 10000,
  intervalTime: { max: 3000, min: 1000 }
})

const requestConfig = [
  { url: 'https://xxx.com/xxxx' },
  { url: 'https://xxx.com/xxxx', method: 'POST', data: { name: 'coderhxl' } },
  { url: 'https://xxx.com/xxxx' }
]

myXCrawl.crawlData({ requestConfig }).then(res => {
   // deal with
})

Crawl files

Crawl file data via crawlFile()

import path from 'node:path'
import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({ 
  timeout: 10000,
  intervalTime: { max: 3000, min: 1000 }
})

const requestConfig = [ 'https://xxx.com/xxxx', 'https://xxx.com/xxxx' ]

myXCrawl
  .crawlFile({
    requestConfig,
    fileConfig: {
      storeDir: path.resolve(__dirname, './upload') // storage folder
    }
  })
  .then((fileInfos) => {
    console.log(fileInfos)
  })

Start polling

Start a polling crawl with startPolling

import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({ 
  timeout: 10000,
  intervalTime: { max: 3000, min: 1000 }
})

myXCrawl. startPolling({ h: 2, m: 30 }, (count, stopPolling) => {
  // will be executed every two and a half hours
  // crawlPage/crawlData/crawlFile
  myXCrawl.crawlPage('https://xxx.com').then(res => {
    const { jsdom, browser, page } = res
    
    // Close the browser
    browser.close()
  })
})

Callback function parameters:

  • The count attribute records the current number of polling operations.
  • stopPolling is a callback function, calling it can terminate subsequent polling operations.

Request interval time

Setting the requests interval time can prevent too much concurrency and avoid too much pressure on the server.

It can be set when creating a crawler instance, or you can choose to set it separately for an API. The request interval time is controlled internally by the instance method, not by the instance to control the entire request interval time.

import xCrawl from 'x-crawl'

// Unified settings
const myXCrawl = xCrawl({
  intervalTime: { max: 3000, min: 1000 }
})

// Set individually (high priority)
myXCrawl.crawlFile({
  requestConfig: [ 'https://xxx.com/xxxx', 'https://xxx.com/xxxx' ],
  intervalTime: { max: 2000, min: 1000 }
})

The intervalTime option defaults to undefined . If there is a setting value, it will wait for a period of time before requesting, which can prevent too much concurrency and avoid too much pressure on the server.

  • number: The time that must wait before each request is fixed
  • Object: Randomly select a value from max and min, which is more anthropomorphic

Note: The first request will not trigger the interval.

Multiple ways of writing requestConfig options

The writing method of requestConfig is very flexible, there are 5 types in total, which can be:

  • string
  • array of strings
  • object
  • array of objects
  • string plus object array
import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({ 
  timeout: 10000,
  intervalTime: { max: 3000, min: 1000 }
})

// requestConfig writing method 1:
const requestConfig1 = 'https://xxx.com/xxxx'

// requestConfig writing method 2:
const requestConfig2 = [ 'https://xxx.com/xxxx', 'https://xxx.com/xxxx', 'https://xxx.com/xxxx' ]

// requestConfig writing method 3:
const requestConfig3 = {
  url: 'https://xxx.com/xxxx',
  method: 'POST',
  data: { name: 'coderhxl' }
}

// requestConfig writing method 4:
const requestConfig4 = [
  { url: 'https://xxx.com/xxxx' },
  { url: 'https://xxx.com/xxxx', method: 'POST', data: { name: 'coderhxl' } },
  { url: 'https://xxx.com/xxxx' }
]

// requestConfig writing method 5:
const requestConfig5 = [
  'https://xxx.com/xxxx',
  { url: 'https://xxx.com/xxxx', method: 'POST', data: { name: 'coderhxl' } },
  'https://xxx.com/xxxx'
]

myXCrawl.crawlData({ requestConfig: requestConfig5 }).then(res => {
  console.log(res)
})

It can be selected according to the actual situation.

Multiple ways to get results

There are three ways to get the result: Promise, Callback and Promise + Callback.

  • Promise: After all requests end, get the results of all requests
  • Callback: After each request ends, get the result of the current request

These three methods apply to crawlPage, crawlData and crawlFile.

import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({
  timeout: 10000,
  intervalTime: { max: 3000, min: 1000 }
})

const requestConfig = [ 'https://xxx.com/xxxx', 'https://xxx.com/xxxx', 'https://xxx.com/xxxx' ]

// Method 1: Promise
myXCrawl
  .crawlFile({
    requestConfig,
    fileConfig: { storeDir: path. resolve(__dirname, './upload') }
  })
  .then((fileInfos) => {
    console.log('Promise: ', fileInfos)
  })

// Method 2: Callback
myXCrawl.crawlFile(
  {
    requestConfig,
    fileConfig: { storeDir: path. resolve(__dirname, './upload') }
  },
  (fileInfo) => {
    console.log('Callback: ', fileInfo)
  }
)

// Method 3: Promise + Callback
myXCrawl
  .crawlFile(
    {
      requestConfig,
      fileConfig: { storeDir: path. resolve(__dirname, './upload') }
    },
    (fileInfo) => {
      console.log('Callback: ', fileInfo)
    }
  )
  .then((fileInfos) => {
    console.log('Promise: ', fileInfos)
  })

It can be selected according to the actual situation.

API

x-crawl

Create a crawler instance via call xCrawl. The request queue is maintained by the instance method itself, not by the instance itself.

Type
function xCrawl(baseConfig?: XCrawlBaseConfig): XCrawlInstance
Example
import xCrawl from 'x-crawl'

// xCrawl API
const myXCrawl = xCrawl({
  baseUrl: 'https://xxx.com',
  timeout: 10000,
  // The interval between requests, multiple requests are valid
  intervalTime: {
    max: 2000,
    min: 1000
  }
})

Note: To avoid repeated creation of instances in subsequent examples, myXCrawl here will be the crawler instance in the crawlPage/crawlData/crawlFile example.

crawlPage

crawlPage is the method of the crawler instance, usually used to crawl page.

Type
function crawlPage: (
  config: CrawlPageConfig,
  callback?: (res: CrawlPage) => void
) => Promise<CrawlPage>
Example
import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({ timeout: 10000 })

// crawlPage API
myXCrawl.crawlPage('https://xxx.com/xxxx').then((res) => {
  const { jsdom, browser, page } = res
  console.log(jsdom.window.document.querySelector('title')?.textContent)
    
  // Close the browser
  browser.close()
})

crawlData

crawlData is the method of the crawler instance, which is usually used to crawl APIs to obtain JSON data and so on.

Type
function crawlData: <T = any>(
  config: CrawlDataConfig,
  callback?: (res: CrawlResCommonV1<T>) => void
) => Promise<CrawlResCommonArrV1<T>>
Example
import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({
  timeout: 10000,
  intervalTime: { max: 2000, min: 1000 }
})

const requestConfig = [
  { url: 'https://xxx.com/xxxx' },
  { url: 'https://xxx.com/xxxx', method: 'POST', data: { name: 'coderhxl' } },
  { url: 'https://xxx.com/xxxx' }
]

// crawlData API
myXCrawl.crawlData({ requestConfig }).then(res => {
  console.log(res)
})

crawlFile

crawlFile is the method of the crawler instance, which is usually used to crawl files, such as pictures, pdf files, etc.

Type
function crawlFile: (
  config: CrawlFileConfig,
  callback?: (res: CrawlResCommonV1<FileInfo>) => void
) => Promise<CrawlResCommonArrV1<FileInfo>>
Example
import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({
  timeout: 10000,
  intervalTime: { max: 2000, min: 1000 }
})

const requestConfig = [ 'https://xxx.com/xxxx', 'https://xxx.com/xxxx' ]

myXCrawl
  .crawlFile({
    requestConfig,
    fileConfig: {
      storeDir: path.resolve(__dirname, './upload') // storage folder
    }
  })
  .then((fileInfos) => {
    console.log(fileInfos)
  })

startPolling

crawlPolling is a method of the crawler instance, typically used to perform polling operations, such as getting news every once in a while.

Type
function startPolling(
  config: StartPollingConfig,
  callback: (count: number, stopPolling: () => void) => void
): void
Example
import xCrawl from 'x-crawl'

const myXCrawl = xCrawl({
  timeout: 10000,
  intervalTime: { max: 2000, min: 1000 }
})

// startPolling API
myXCrawl.startPolling({ h: 2, m: 30 }, (count, stopPolling) => {
  // will be executed every two and a half hours
  // crawlPage/crawlData/crawlFile
})

Types

AnyObject

interface AnyObject extends Object {
  [key: string | number | symbol]: any
}

Method

type Method = 'get' | 'GET' | 'delete' | 'DELETE' | 'head' | 'HEAD' | 'options' | 'OPTONS' | 'post' | 'POST' | 'put' | 'PUT' | 'patch' | 'PATCH' | 'purge' | 'PURGE' | 'link' | 'LINK' | 'unlink' | 'UNLINK'

RequestConfigObject

interface RequestConfigObject {
  url: string
  method?: Method
  headers?: AnyObject
  params?: AnyObject
  data?: any
  timeout?: number
  proxy?: string
}

RequestConfig

type RequestConfig = string | RequestConfigObject

MergeRequestConfigObject

interface MergeRequestConfigObject {
  url: string
  timeout?: number
  proxy?: string
}

IntervalTime

type IntervalTime = number | {
  max: number
  min?: number
}

XCrawlBaseConfig

interface XCrawlBaseConfig {
  baseUrl?: string
  timeout?: number
  intervalTime?: IntervalTime
  mode?: 'async' | 'sync'
  proxy?: string
}

CrawlBaseConfigV1

interface CrawlBaseConfigV1 {
  requestConfig: RequestConfig | RequestConfig[]
  intervalTime?: IntervalTime
}

CrawlPageConfig

type CrawlPageConfig = string | MergeRequestConfigObject

CrawlDataConfig

interface CrawlDataConfig extends CrawlBaseConfigV1 {
}

CrawlFileConfig

interface CrawlFileConfig extends CrawlBaseConfigV1 {
  fileConfig: {
    storeDir: string // Store folder
    extension?: string // Filename extension
  }
}

StartPollingConfig

interface StartPollingConfig {
  d?: number // day
  h?: number // hour
  m?: number // minute
}

XCrawlInstance

interface XCrawlInstance {
  crawlPage: (
    config: CrawlPageConfig,
    callback?: (res: CrawlPage) => void
  ) => Promise<CrawlPage>

  crawlData: <T = any>(
    config: CrawlDataConfig,
    callback?: (res: CrawlResCommonV1<T>) => void
  ) => Promise<CrawlResCommonArrV1<T>>

  crawlFile: (
    config: CrawlFileConfig,
    callback?: (res: CrawlResCommonV1<FileInfo>) => void
  ) => Promise<CrawlResCommonArrV1<FileInfo>>

  startPolling: (
    config: StartPollingConfig,
    callback: (count: number, stopPolling: () => void) => void
  ) => void
}

CrawlResCommonV1

interface CrawlCommon<T> {
  id: number
  statusCode: number | undefined
  headers: IncomingHttpHeaders // nodejs: http type
  data: T
}

CrawlResCommonArrV1

type CrawlResCommonArrV1<T> = CrawlResCommonV1<T>[]

FileInfo

interface FileInfo {
  fileName: string
  mimeType: string
  size: number
  filePath: string
}

CrawlPage

interface CrawlPage {
  httpResponse: HTTPResponse | null // The type of HTTPResponse in the puppeteer library
  browser // The type of Browser in the puppeteer library
  page: Page // The type of Page in the puppeteer library
  jsdom: JSDOM // The type of JSDOM in the jsdom library
}

More

If you have any questions or needs , please submit Issues in https://github.com/coder-hxl/x-crawl/issues .

Keywords

FAQs

Package last updated on 07 Mar 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc