1.0.13 • Published 10 years ago
node-scraper v1.0.13
#node-scraper
A simple web scraping module.
var scraper = new Scraper('http://www.urbandictionary.com');
scraper.scrape().on('done', function(err, statusCode, content){
if (err){
console.error(err);
}
else {
console.log(statusCode, content);
}
});
or if we want to be more precise
var scraper = new Scraper('http://www.urbandictionary.com', {
selectors: ['.word', '.meaning']
});
scraper.scrape().on('done', function(err, statusCode, content){
if (err){
console.error(err);
}
else {
console.log(statusCode, content);
}
});
and if we also want the value of some attributes for a selected element, we can append the set of the desired attributes (in curly braces) to the selector.
var scraper = new Scraper('http://www.urbandictionary.com', {
selectors: ['.word{href}', '.meaning']
});
scraper.scrape().on('done', function(err, statusCode, content){
if (err){
console.error(err);
}
else {
console.log(statusCode, content);
}
});
what the the output might look like:
[
{
"selector": ".word",
"content": [
{
"html": "No pun intended",
"attributes": {
"href": "/define.php?term=No+pun+intended&defid=8320127"
}
},
{
"html": "hashtalk",
"attributes": {
"href": "/define.php?term=hashtalk&defid=8320335"
}
}
]
},
{
"selector": ".meaning",
"content": [
{
"html": " Pun intended. (and now acknowledged) "
},
{
"html": " When hashtags are spoken out loud. "
}
]
}
]
tests
Have a look at the tests for more usage examples.
$ grunt test
docs
Or generate and read the docs.
$ grunt docs