Phantomjs: Phantomjs 无法显示 — 或其他一些 UTF-8 字词。

创建于 2014-03-16  ·  3评论  ·  资料来源: ariya/phantomjs

我编写了一个 url2src.js 来将一个 html 文件转换为另一个处理所有 javascripts 的 html 文件。
我发现如果有一些UTF-8字(比如-),结果是不正确的。

我用的是Windows 8.1中文版。 并在 Windows cmd 中运行此命令:

d:\epub\components>phantomjs  --output-encoding=utf8 --script-encoding=utf8 url2src.js activities2.html activities2-processed.html
d:\epub\components>phantomjs --version
1.9.7

url2src.js 的内容是:

var page = require('webpage').create(),
    system = require('system'),
    t, address, output;

if (system.args.length !== 3) {
    console.log('Usage: url2src.js <some URL> <output File path>');
    phantom.exit();
}

t = Date.now();
address = system.args[1];
output = system.args[2];
page.open(address, function (status) {
    if (status !== 'success') {
        console.log('FAIL to load the address : ' + address);
    } else {
        t = Date.now() - t;
        //console.log('Loading time ' + t + ' ms');
        var js = page.evaluate(function () {
            return document;
        });
        //console.log(js.all[0].outerHTML); 
        var fs = require('fs');
        try {
            fs.write(output, js.all[0].outerHTML, 'w');
        } catch(e) {
            console.log(e);
        }
    }
    phantom.exit();
});

更多详细信息在此处的附件中:
https://groups.google.com/forum/#!topic/phantomjs/oqvK8mkk6aY

任何帮助表示赞赏。

最有用的评论

我现在知道原因了,因为输入的 html 文档默认被视为 ISO-8859-1。
添加meta设置charset=utf-8可以解决这个问题。

<html>
<head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 </head>
 <body>
    <p> The user interface for an activity is provided by a hierarchy of views—objects derived from the <code>View</code> class. </p> 
 </body>
</html>

所有3条评论

我现在知道原因了,因为输入的 html 文档默认被视为 ISO-8859-1。
添加meta设置charset=utf-8可以解决这个问题。

<html>
<head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 </head>
 <body>
    <p> The user interface for an activity is provided by a hierarchy of views—objects derived from the <code>View</code> class. </p> 
 </body>
</html>

你说得对,但是别人给我的html我不能改,怎么办?

我有一些没有任何标题信息的文档(我从第三方应用程序获取它们)......强制编码的方法会很好。

此页面是否有帮助?
0 / 5 - 0 等级