1

I'm setting up a web scraper using Node.js and want to grab some html from a url and save it as a variable. A stripped down version follows.

var request = require('request');
var get_html = function(){
    var url = "http://www.google.com";
    var html = '';
    request.get(url,function(error, response, body){
        html += body;
    });
    return html;
};
console.log(get_html());

It seems that the function returns before request can concatenate the html to the variable html. As far as I can see, request only allows me to manipulate the html within the callback function or pipe it to a file. Is there anyway to just return it as a variable?

2 Answers 2

1

request.get is asynchronous and it will return result in the callback function.

You need to adapt your code a little bit like this

var request = require('request');

// get_html receive callback to process result
var get_html = function(callback) {
    var url = "http://www.google.com";
    var html = '';
    request.get(url,function(error, response, body){
        return callback(body); // call callback and parse result to it
    });
};

// call get_html function
// and log html result here
get_html(function (body) { console.log(body); });

Code with a lot of function callbacks looks not beautiful. I prefer promise than callback. If you wish to use promise, try 'request-promise' lib.

Sign up to request clarification or add additional context in comments.

1 Comment

So it looks like there's no way to actually return the html as a string, only to pass the rest of the code into request as a callback? I mean to use this small function as a utility that can be called by different modules. It sounds like this will get unwieldy quickly.
0

It appears that request.get is async, so you have to put return html; in the callback. Otherwise it's returning instantly, before request.get can finish running.

Comments

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.