【问题标题】:x-ray: Read html from a file rather than a URLx-ray:从文件而不是 URL 中读取 html
【发布时间】:2017-07-19 07:36:10
【问题描述】:

代码

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-ray 中,而不是将其指向远程 URL?

【问题讨论】:

    标签: javascript html file x-ray


    【解决方案1】:

    This example 来自 X 射线回购解决了我的问题。只需传递一个 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);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      相关资源
      最近更新 更多