Creating an html document which can be manipulated with the jquery node.js module

Creating an html document which can be manipulated with the jquery node.js module

Problem

I have a string of html from a $.ajax() request (using the jquery node.js package) that I want to scrape data from. From the documentation in the readme it would seem that var $ = require('jquery'); creates a empty html document. Is it possible to replace that DOM with the html string so you can interact with it with jQuery like you would a typical website?

Problem courtesy of: forwardslash

Solution

Sure:

var $ = require('jquery');

$.get(URL, function(html) {
  var $doc = $(html);
  $doc.find('a').each(function(i, el) {
    console.log('href', $(el).attr('href'));
  });
});
Solution courtesy of: robertklep

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *