3

I would like to scrape the http://www.euromillones.com.es/ website to get the last winning 5 numbers and two stars. It can be seen on the left column of the website. I've been reading tutorials, but I am not capable of achieving this.

This is the code I wrote 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:

Must pass a "created", "loaded", "done" option or a callback to jsdom.env.

1 Answer 1

7

It looks to me that you've just used a combination of arguments that jsdom does not know how to handle. The documentation shows this signature:

jsdom.env(string, [scripts], [config], callback);

The two middle arguments are optional but you'll note that all possible combinations here start with a string and end with a callback. The documentation mentions one more way to call jsdom.env, and that's by passing a single config argument. What you are doing amounts to:

jsdom.env(config, callback);

which does not correspond to any of the documented methods. I would suggest changing your code to pass a single config argument. You can move your current callback to the done field of that config object. Something like this:

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());
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

this is how i solved it: jsdom.env({ url: "euromillones.com.es", scripts: ['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()); } });
2018 called and said: "jsdom.env is undefined"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.