1

代码

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);
});

我的目标

我正在使用 X 射线从网站上抓取一些日期。出于测试和开发目的,我想从本地文件而不是远程资源解析数据。

如何将本地文件加载到 X 射线中,而不是将其指向远程 URL?

4

1 回答 1

1

这个来自 x-ray repo 的例子解决了我的问题。只需传递一个 HTML 字符串而不是 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);
});
于 2017-07-19T07:36:10.327 回答