Created
August 26, 2016 15:39
-
-
Save loretoparisi/32965788248aedba4ab20d0064a16e23 to your computer and use it in GitHub Desktop.
JSDOM + Wgxpath vs Cheerio + Request HTML Parsers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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