【问题标题】:node.js \ how to treat to result of sanitize-htmlnode.js \ 如何处理sanitize-html的结果
【发布时间】:2015-10-16 15:34:25
【问题描述】:

Node.js 中的新手 我尝试使用 sanitize-html 模块清理 node.js 中的 html - 我认为这个问题看起来更通用:

插件输出一个对象(我打印到控制台并显示 [object]) - 我怎么知道如何使用这个对象?它的字段是什么,如何将其写入文件等(我知道这听起来很基本 - 我应该序列化它吗?使用对象的方法是什么......)

var Crawler = require("js-crawler");
var download = require("url-download");
var sanitizeHtml = require('sanitize-html');
var util = require('util');
var fs = require('fs');

new Crawler().configure({depth: 1})
  .crawl("http://www.cnn.com", function onSuccess(page) {

    var clean = sanitizeHtml(page);
    console.log(clean);
    fs.writeFile('sanitized.txt', clean, function (err) {
        if (err) throw err;
        console.log('It\'s saved! in same location.');
    });

    console.log(util.inspect(clean, {showHidden: false, depth: null}));
    var str = JSON.stringify(clean.toString());
    console.log(str);
    /*download(page.url, './download')
    .on('close', function () {
      console.log('One file has been downloaded.');
    });*/
  });

【问题讨论】:

  • 请出示您的代码。
  • 已附上。在这里尝试了很多东西 - 如何获取“干净”变量的内容??

标签: node.js html-sanitizing


【解决方案1】:

您的问题不在于 'sanitize-html。您没有正确对待 page 变量。你应该使用:

var clean = sanitizeHtml(page.body);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    相关资源
    最近更新 更多