Saturday, March 15, 2014

Making a scraper in Node.js...

Let's make a multi link crawler for a multi page query in a web page listing jobs opportunities.

First let's make a module of it, in a separate file from our server in node whatever it is and we gonna call it with

var worker = require('worker.js');

It will be called with this sentence from our express router or whatever you are using.

First we gonna need two libraries 

var request = require("request");
var cheerio = require("cheerio");

One for making requests more easy(request) and another for making jQuery available on the server side(cheerio).

Now we need the list of pages of the main web site, this one makes a default paged list with the jobs listed that day in in one link, and paginates on base of that link so...

var url = "http://www.bumeran.com.ar/empleos-publicacion-hoy.html";

Now for exporting this function to the node server we gona make a "start" function and export it.

In this example we gona make a request to the url of before and read the body with cheerio taking the 
number of pages of that list from the paginator in the bottom. 

Then we send that number of pages to the scraper function 

exports.start = function(req, res) {
    request(url, function() {
        return function(err, resp, body) {
            if (err && resp.statusCode == 200) {
                console.log(err); //throw err;
            }
            $ = cheerio.load(body);
            var pages = $(".paginador.box a:nth-last-child(2)").text().trim();
            console.log(pages); 
            scraper(pages);
        };
    });
};  

The function scraper must have the variable url by the number of page in this case it gona be:

var url = "http://www.bumeran.com.ar/empleos-publicacion-hoy-pagina-" + NUMBEROFPAGE + ".html";

The function scraper will only recive the number of pages from the scraping from before.

It will read every list in every page, and send specific links to the pages with the job descriptions to the scraperLinks function this process will be asynchronously by the nature of node.js. 

function scraper(pages) {
    for(var i = 0; i < pages ; i++){
        var url = "http://www.bumeran.com.ar/empleos-publicacion-hoy-pagina-" + (i + 1) + ".html";
        request(url, ( function(i) {
            return function(err, resp, body) {
                if (err && resp.statusCode == 200){
                  console.log(err); //throw err;
                }
                $ = cheerio.load(body);
                $(".aviso_box.aviso_listado").each(function(index, tr) {
                    console.log("Scrapping..." + $(this).attr("href")); 
                    scraperLinks($(this).attr("href"));
                });
            };
        })(i));
    }
}
NOTE: We are sending the value from "i" to the function as a value itself not a ref because it would not work as expected for the asynchronously way of work.


For last scraperLinks will get every link to a details page and get the info we need with plain jQuery:

function scraperLinks(link) {
            var url = "http://www.bumeran.com.ar" + link;
            request(url, function(err, resp, body) {
                    if (err && resp.statusCode == 200) { console.log(err); } //throw err;
                    $ = cheerio.load(body);            //console.log(body); 
                    var location = $('.aviso-resumen-datos tr td').last().text().trim(); // $('#.aviso-resumen-datos tr').last().find( "a" ).text();
                    var detail = $("#contenido_aviso p:nth-child(2)").text();//$("#contenido_aviso p").first().text();
                    var title = $(".box h2").first().text().trim();
                    var date = $(".aviso-resumen-datos tbody tr td").first().text().trim();
                    console.log("Saving..." + url); 
                    saveAd(url, location, detail, title, date);
                });
        }  

In this case we get title, date, and details from the published job, and send it to the saveAdd funtion that will recive the values and store them in any way posible. 

Well that's all 

3 comments:

edsoniglesia said...

That said, when you make a deposit, you could declare different promos that allow you to to boost your on line casino bankroll. Some on line casino sites supply this as a part of} a welcome or reload bonus. To win money, you would have to 1xbet korea to} play real money slots, and never their free-to-play version. When talking concerning the handy features of a on line casino, banking is certainly one of them.

Jackie Co Kad said...

Building a multi-page web scraper with Node.js is a practical way to learn how web requests, HTML parsing, and asynchronous operations work together. Using libraries such as Request and Cheerio allows developers to retrieve web pages, extract pagination details, and process listings across multiple pages. Separating the scraping logic into a reusable module also improves code organization and makes it easier to integrate the crawler into an Express application.

Node.js is well suited to building data collection tools and backend services that handle multiple HTTP requests. Understanding modules, asynchronous programming, request handling, and data extraction helps developers create maintainable server-side applications. Those interested in strengthening their Node.js development skills can explore Nodejs Online Training in India.

Jackie Co Kad said...

Integrating a crawler with Express provides a useful opportunity to learn how routing, reusable services, and backend processing fit together in a web application. Developers can build on these fundamentals to create APIs that organize and expose collected data while handling errors and respecting website access policies. Learners looking to develop their backend application skills can explore Express JS Online Training in India.

The Video converter I needed

Handbrake it's an open-source video transcoder and a lifesaver!  For people that don't have a lot of time for transcoding or don...