x-ray: Read html from a file rather than a URL

272 Views Asked by At

Code

const Xray = require('x-ray');

const xray = Xray();

// How do I read a file, rather than a URL?
const url = 'https://www.tel-o-fun.ga/';

xray(url, '.marker')((err, value) => {
  console.log(value);
});

My goal

I am using x-ray to scrape some date from a website. For testing and development purposes, I would like to parse data from a local file rather than a remote resource.

How do I load a local file into x-ray, instead of pointing it to a remote URL?

1

There are 1 best solutions below

0
On

This example from the x-ray repo solved my problem. Simply pass an HTML string instead of a URL:

const path = require('path');
const Xray = require('x-ray');
const read = require('fs').readFileSync;

const html = read(path.resolve(__dirname, 'index.html'));
const xray = Xray();

xray(html, '.marker')((err, value) => {
  console.log(value);
});