Need to get a word from the site http://sluchajnoe.ru/slovo.php

var req = require("request"); var iconv = require('iconv-lite'); req("http://sluchajnoe.ru/slovo.php", function (error, res, body){ var wordRandom = iconv.decode(new Buffer(body), "UTF-8"); wordRandom = wordRandom.replace(/<font size="200%">(.+)/gi, function(match, p1){ console.log(p1); return p1; }); }); 

The code gives krakazyabra. What needs to be fixed?

    1 answer 1

    You can initially take data as a Buffer . To do this, call request with the encoding: null parameter.

    The page itself is given as 'text/html; charset=CP1251' 'text/html; charset=CP1251' , respectively, and decode should be done with win1251 .

    This is how it should work:

     var req = require("request"); var iconv = require('iconv-lite'); req( { url: "http://sluchajnoe.ru/slovo.php", encoding: null }, function (error, res, body){ var wordRandom = iconv.decode(body, "win1251"); wordRandom = wordRandom.replace(/<font size="200%">(.+)/gi, function(match, p1){ console.log(p1); return p1; }); });