Node.js Web Scraper with Jsdom
I would like to clear the site http://www.euromillones.com.es/ to get the last winning 5 numbers and two stars. This can be seen in the left column of the website. I have read tutorials but I am not capable of this.
This is the code I have written so far:
app.get('/winnernumbers', function(req, res){
//Tell the request that we want to fetch youtube.com, send the results to a callback function
request({uri: 'http://www.euromillones.com.es/ '}, function(err, response, body){
var self = this;
self.items = new Array();//I feel like I want to save my results in an array
//Just a basic error check
if(err && response.statusCode !== 200){console.log('Request error.');}
//Send the body param as the HTML code we will parse in jsdom
//also tell jsdom to attach jQuery in the scripts and loaded from jQuery.com
jsdom.env({
html: body,
scripts: ['http://code.jquery.com/jquery-1.6.min.js ']
}, function(err, window){
//Use jQuery just as in a regular HTML page
var $ = window.jQuery;
res.send($('title').text());
});
});
});
I am getting the following error:
You must pass the "created", "uploaded", "made" or link back to jsdom.env.
source to share
It looks to me like you just used a combination of arguments that jsdom doesn't know how to handle. The documentation shows this signature:
jsdom.env(string, [scripts], [config], callback);
The two middle arguments are optional, but you'll notice that all possible combinations here start with a string and end with a callback. The documentation mentions another way to call jsdom.env
and also pass one argument config
. What you are doing is:
jsdom.env(config, callback);
which doesn't match any of the documented methods. I would suggest changing my code to pass a single config argument. You can move the current callback to the field of done
this config object. Something like that:
jsdom.env({
html: body,
scripts: ['http://code.jquery.com/jquery-1.6.min.js'],
done: function (err, window) {
//Use jQuery just as in a regular HTML page
var $ = window.jQuery;
res.send($('title').text());
}
});
source to share