Skip to content

Instantly share code, notes, and snippets.

@black1277
Last active May 12, 2025 12:50
Show Gist options
  • Save black1277/59819223d55b466d69b18109b5ae8e7f to your computer and use it in GitHub Desktop.
Save black1277/59819223d55b466d69b18109b5ae8e7f to your computer and use it in GitHub Desktop.
Преобразование полученных данных в свою кодировку
// Вариант 1
fetch('https://www.site.ru/')
.then((response) => response.arrayBuffer())
.then((buffer) => {
let html = new TextDecoder('windows-1251').decode(buffer);
let doc = new DOMParser().parseFromString(html, 'text/html');
let b = doc.querySelectorAll(".news_item p")[0].innerHTML;
console.log(b);
});
// Вариант 2
fetch('https://www.site.ru/').then(function(promise) {
return promise.blob();
}).then(function(file) {
var reader = new FileReader();
reader.readAsText(file, 'windows-1251');
reader.onload = function() {
var doc = new DOMParser().parseFromString(reader.result, 'text/html');
var b = doc.querySelectorAll(".news_item p")[0].innerHTML;
console.log(b);
};
});
// для Node.js
// npm i iconv-lite request
const request = require('request'),
fs = require('fs'),
iconv = require('iconv-lite');
request({
url: 'https://litportal.ru/trial/txt/6376244.txt',
encoding: null,
body: 'Buffer'
}).pipe(iconv.decodeStream('win1251'))
.pipe(iconv.encodeStream('utf8'))
.pipe(fs.createWriteStream('file-in-utf8.txt'));
@black1277
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment