LoginSignup
2
2

More than 5 years have passed since last update.

cheerioで日本語を読み込ませた時にhtml()を使うといい感じに表示出来ない

Last updated at Posted at 2015-01-21

cheerioとはこちら

htmlを読み込ませると、jQueryっぽく使えるようにしてくれるもの。

そして、.html()を使うと日本語がちゃんと出なかった。。。

var cheerio = require('cheerio');

var contents = '<div id="hoge">あああ</div>';
var $ = cheerio.load(contents);

// 以下日本語は変換されない
console.log($.html());
console.log($('#hoge').html());

// 以下ちゃんと出る
console.log($('#hoge').text());

出力結果

<div id="hoge">&#x3042;&#x3042;&#x3042;</div>
&#x3042;&#x3042;&#x3042;
あああ

.html()を使うと、日本語にちゃんと変換されてない。。。

.text()を使うと、正常に出力される。

とりあえず、表示させよう〜。的なノリで.html()を使ったら日本語出ないから、なんでやー!と、無駄にハマってしまった。。。今日この頃です。

cheerioのversionは0.18.0だった。

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2