Question

The code is simple. I'm only interested in the first part of the source code of youtube.com and I'd like to stop downloading data when content-length is higher than 10000, for example. I've been trying to do it with no success. Any ideas?

var request = require('request');
var cheerio = require('cheerio');
var url = 'https://www.youtube.com/';


request(url, function(err, resp, body) {
  if (err)
      throw err;
    $ = cheerio.load(body);

});
Was it helpful?

Solution

Don't use callback, it's fired only when request is completed, and your request will never be.

Use streams:

var request = require('request')
var url = 'https://www.youtube.com/'

var stream = request({
   url: url,
   encoding: 'utf8'
})

var len = 0
var result = ''
stream.on('data', function(d) {
   len += Buffer.byteLength(d)
   result += d
   if (len > 1000) {
      stream.abort()
      console.log(d)
   }
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top