Phantomjs: Phantomjsは表示できません—または他のUTF-8単語。

作成日 2014年03月16日  ·  3コメント  ·  ソース: ariya/phantomjs

url2src.jsを記述して、htmlファイルをすべてのjavascriptが処理される別のhtmlファイルに変換します。
UTF-8の単語(—など)がある場合、結果が正しくないことがわかりました。

私はWindows8.1の中国語版を使用しています。 そして、Windowscmdで次のコマンドを実行します。

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として扱われたため、私は今その理由を知っています。
set 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として扱われたため、私は今その理由を知っています。
set 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 評価