Answer the question
In order to leave comments, you need to log in
How to parse images via a NodeJs GET request?
I've been sitting for more than 9 hours, help me figure it out.
So everything works great, the problem is that I want to check if there is an image at this url or not? And it overwrites and I can't do it.
var request = require('request'),
cheerio = require('cheerio'),
sys = require('sys'),
http = require('http'),
token = require('token'),
crypto = require('crypto'),
net = require('net'),
get = require('get'),
Iconv = require('iconv').Iconv;
for (var i = 0; i < 5; i++) { // неудачная попытка сделать цикл, хотя работает но request.get перезаписивает
// переменную и происходит такая ситуация что реально проверяется только одна ссылка на картинку
var token = crypto.randomBytes(16).toString('hex'); // генерация мд5 на угад парсить думаю
var url = 'https://wu.wsiz.rzeszow.pl/wunet/photos/s' + token + '.jpg';
request.get({uri: url, method: 'GET', encoding: 'binary' }, function (err, res, body) {
console.time(url);
body = new Buffer(body, 'binary');
var iconv = new Iconv('latin1', 'utf8//IGNORE');
body = iconv.convert(body).toString();
var $ = cheerio.load(body);
var tmp = $('h1').html();
// Если есть h1 значит попало на ошибку и изображения тут нету, проверка ниже
if (tmp==null)
console.log('ok='+url+'##############################################');
// Если заголовка нету, значит есть изображения
else {
console.log('fuck %)'+ token);
}
console.timeEnd(url);
});}
Answer the question
In order to leave comments, you need to log in
Or read what a closure is or this code won't work. Or you can't declare functions inside a loop, or you can't write var inside a loop. Or you learn JavaScript here: learn.javascript.ru or node here: learn.javascript.ru/nodejs-screencast and then labs here: nodeschool.io
var request = require('request'),
crypto = require('crypto');
var token, url;
for (var i = 0; i < 5; i++) {
token = crypto.randomBytes(16).toString('hex');
url = 'https://wu.wsiz.rzeszow.pl/wunet/photos/s' + token + '.jpg';
doRequest(url);
}
function doRequest(url) {
request.get(
{ uri:url, method:'GET', encoding:'binary' },
function (err, res, body) {
console.time(url);
body = new Buffer(body, 'binary');
console.timeEnd(url);
}
);
}
It looks like you just run the loop to the end and the process ends. The node is asynchronous, control on request.get is not blocked. Read about async and promises.
Thank you very much! Tell me, how can the memory be freed, because with a large cycle - it gives an error that there is not enough memory ... Can I use this module?
https://github.com/nodejitsu/forever
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question