g_rmz
g_rmz

Reputation: 741

Access a web page from Node script on desktop

I know that this could be a very stupid question, but, since I'm totally new to Javascript, I'm not sure about how to do this. I want to write a script and run it through node on my laptop, and, in this script, I want to interact with a web page in order to use functions like document.getElementById and stuff like that.

In Python one could do this by using something like Beautiful Soup or requests, but how do you do this in Javascript?

Upvotes: 1

Views: 33

Answers (1)

Stephan Köninger
Stephan Köninger

Reputation: 314

I have implemented a crawler using cheerio and request-promise as follows:

https://www.npmjs.com/package/cheerio

let request = require('request-promise');
let cheerio = require('cheerio');

request = request.defaults({
    transform: function (body) {
        return cheerio.load(body);
    }
});

// ... omitted

request({uri: 'http://example.org'})
    .then($ => {
        const element = $('.element-with-class');
    });

Upvotes: 1

Related Questions