Skip to content
The next web scraper. See through the <html> noise.
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Create stale.yml Jul 15, 2019
lib Fix array stream to account for empty arrays (#337) Jul 7, 2019
test Fix array stream to account for empty arrays (#337) Jul 7, 2019
.gitignore Update travis phantom pattern Mar 20, 2016
.npmignore remove coverage from git May 31, 2015
.travis.yml require node 6.0.0 or higher Jun 30, 2019 Release 2.3.3 Jun 22, 2018
LICENSE Create LICENSE Apr 17, 2019 Add gitter room to the README Jul 15, 2019
index.js Fix promise compatibility Jul 2, 2019
package.json Update dependency coveralls to v3.0.6 (#351) Aug 17, 2019


Last version Build Status Coverage Status Dependency status Dev Dependencies Status NPM Status Node version OpenCollective OpenCollective Gitter

var Xray = require('x-ray')
var x = Xray()

x('', '.post', [
    title: 'h1 a',
    link: '.article-title@href'
  .paginate('.nav-previous a@href')


npm install x-ray


  • Flexible schema: Supports strings, arrays, arrays of objects, and nested object structures. The schema is not tied to the structure of the page you're scraping, allowing you to pull the data in the structure of your choosing.

  • Composable: The API is entirely composable, giving you great flexibility in how you scrape each page.

  • Pagination support: Paginate through websites, scraping each page. X-ray also supports a request delay and a pagination limit. Scraped pages can be streamed to a file, so if there's an error on one page, you won't lose what you've already scraped.

  • Crawler support: Start on one page and move to the next easily. The flow is predictable, following a breadth-first crawl through each of the pages.

  • Responsible: X-ray has support for concurrency, throttles, delays, timeouts and limits to help you scrape any page responsibly.

  • Pluggable drivers: Swap in different scrapers depending on your needs. Currently supports HTTP and PhantomJS driver drivers. In the future, I'd like to see a Tor driver for requesting pages through the Tor network.

Selector API

xray(url, selector)(fn)

Scrape the url for the following selector, returning an object in the callback fn. The selector takes an enhanced jQuery-like string that is also able to select on attributes. The syntax for selecting on attributes is selector@attribute. If you do not supply an attribute, the default is selecting the innerText.

Here are a few examples:

  • Scrape a single tag
xray('', 'title')(function(err, title) {
  console.log(title) // Google
  • Scrape a single class
xray('', '.content')(fn)
  • Scrape an attribute
xray('', 'img.logo@src')(fn)
  • Scrape innerHTML
xray('', 'body@html')(fn)

xray(url, scope, selector)

You can also supply a scope to each selector. In jQuery, this would look something like this: $(scope).find(selector).

xray(html, scope, selector)

Instead of a url, you can also supply raw HTML and all the same semantics apply.

var html = '<body><h2>Pear</h2></body>'
x(html, 'body', 'h2')(function(err, header) {
  header // => Pear



Specify a driver to make requests through. Available drivers include:

  • request - A simple driver built around request. Use this to set headers, cookies or http methods.
  • phantom - A high-level browser automation library. Use this to render pages or when elements need to be interacted with, or when elements are created dynamically using javascript (e.g.: Ajax-calls).

Returns Readable Stream of the data. This makes it easy to build APIs around x-ray. Here's an example with Express:

var app = require('express')()
var x = require('x-ray')()

app.get('/', function(req, res) {
  var stream = x('', 'title').stream()


Stream the results to a path.

If no path is provided, then the behavior is the same as .stream().


Constructs a Promise object and invoke its then function with a callback cb. Be sure to invoke then() at the last step of xray method chaining, since the other methods are not promisified.

x('', '', [
    title: '.dribbble-img strong',
    image: '.dribbble-img [data-src]@data-src'
  .then(function(res) {
    console.log(res[0]) // prints first result
  .catch(function(err) {
    console.log(err) // handle error in promise


Select a url from a selector and visit that page.


Limit the amount of pagination to n requests.


Abort pagination if validator function returns true. The validator function receives two arguments:

  • result: The scrape result object for the current page.
  • nextUrl: The URL of the next page to scrape.

xray.delay(from, [to])

Delay the next request between from and to milliseconds. If only from is specified, delay exactly from milliseconds.


Set the request concurrency to n. Defaults to Infinity.

xray.throttle(n, ms)

Throttle the requests to n requests per ms milliseconds.

xray.timeout (ms)

Specify a timeout of ms milliseconds for each request.


X-ray also has support for selecting collections of tags. While x('ul', 'li') will only select the first list item in an unordered list, x('ul', ['li']) will select all of them.

Additionally, X-ray supports "collections of collections" allowing you to smartly select all list items in all lists with a command like this: x(['ul'], ['li']).


X-ray becomes more powerful when you start composing instances together. Here are a few possibilities:

Crawling to another site

var Xray = require('x-ray')
var x = Xray()

x('', {
  main: 'title',
  image: x('#gbar a@href', 'title') // follow link to google images
})(function(err, obj) {
    main: 'Google',
    image: 'Google Images'

Scoping a selection

var Xray = require('x-ray')
var x = Xray()

x('', {
  title: 'title',
  items: x('.item', [
      title: '.item-content h2',
      description: '.item-content section'
})(function(err, obj) {
    title: '',
    items: [
        title: 'The 100 Best Children\'s Books of All Time',
        description: 'Relive your childhood with TIME\'s list...'


Filters can specified when creating a new Xray instance. To apply filters to a value, append them to the selector using |.

var Xray = require('x-ray')
var x = Xray({
  filters: {
    trim: function(value) {
      return typeof value === 'string' ? value.trim() : value
    reverse: function(value) {
      return typeof value === 'string'
        ? value
        : value
    slice: function(value, start, end) {
      return typeof value === 'string' ? value.slice(start, end) : value

x('', {
  title: 'title | trim | reverse | slice:2,3'
})(function(err, obj) {
    title: 'oi'


In the Wild

  • Levered Returns: Uses x-ray to pull together financial data from various unstructured sources around the web.



Support us with a monthly donation and help us continue our activities. [Become a backer]


Become a sponsor and get your logo on our website and on our README on Github with a link to your site. [Become a sponsor]



You can’t perform that action at this time.