0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

fetchでeuc-jpページを丸ごととってきてutf-8に変換して書き出す。

Posted at

fetch(`test.html`)
    .then(res => res.blob()) //blobとして読み込みむ
    .then(res => {
        return new Promise(resolve => {
            const reader = new FileReader();
            reader.onload = () => { resolve(reader.result) };
            reader.readAsText(res, 'euc-jp'); // 第2引数に、任意で文字エンコーディングを指定し、文字列として読み込む
        });
    }).then(res => {
        const document = new DOMParser().parseFromString(res, 'text/html'); // HTMLDocument (Document) が返る
        console.log(document.getElementById('id名').innerText);
    });

一度 Blob として読み込み、文字コードを指定して文字列に変換し、そこから DOM に parse する

参考
FileReader.readAsText() - 文字列として読み込む
DOMParser
Fetch API で Shift_JIS の HTML をDOM として読み込む

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?