Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

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

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×