0

I am trying to use Cheerio with the Node.js request library to retrieve metadata about images. It looks like when I make an HTTP GET request to an URL that ends in .jpg, .png, etc, it will send back the whole file and I can't access the HTML in the response. So, my question is, given a URL to an image, how do I read just the HTML or metadata instead of downloading the whole image file upon making a request to the URL?

For example, here is some simple code I have:

    var request = require('request');
    var cheerio = require('cheerio'); // cheerio is just used to parse HTML on the server, like jquery for the server


    request('http://l.yimg.com/os/mit/media/m/content_index/images/sidekick_tv_news-2e9c408.png',function(err,response,body){

        var $ = cheerio.load(body);

        //here it seems like the body is not HTML but all the data pertaining to the image itself - I just want the typical HTML response, not a picture file


        });

does anyone know what I am talking about?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

1

For starters you can use the image url in an img element:

var img = cheerio('<img src="' + imageUrl + '"></img>');

or

var img = cheerio.load('<img src="' + imageUrl + '"></img>');

Then you might be able to perform your queries

Meir
  • 14,081
  • 4
  • 39
  • 47