Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Created August 26, 2016 15:39
Show Gist options
  • Select an option

  • Save loretoparisi/32965788248aedba4ab20d0064a16e23 to your computer and use it in GitHub Desktop.

Select an option

Save loretoparisi/32965788248aedba4ab20d0064a16e23 to your computer and use it in GitHub Desktop.
JSDOM + Wgxpath vs Cheerio + Request HTML Parsers
// wgxpath + jsdom
var wgxpath = require('wgxpath');
var jsdom = require('jsdom');
// cheerio + request
var cheerio = require('cheerio');
var request = require('request');
var url = 'http://www.merriam-webster.com/word-of-the-day';
var expressionString = '//*[@class="word-and-pronunciation"]/h1';
jsdom.env({
url: url,
done: function(errors, window) {
// using wgxpath
wgxpath.install(window);
var expression = window.document.createExpression(expressionString);
var result = expression.evaluate(window.document,
wgxpath.XPathResultType.STRING_TYPE);
if (result.stringValue.length <= 0)
console.log('Failed to get the word of the day.');
else
console.log('The Word of the Day is "' + result.stringValue + '."');
}
});
request(url, function(err, resp, body){
$ = cheerio.load( body );
var result=$('div[class=word-and-pronunciation] h1');
if (!result)
console.log('Failed to get the word of the day.');
else
console.log('The Word of the Day is "' + result.text() + '."');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment