2

I am new to node.js. I created a file named events_scraper.js and put this code in that file:

var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
var regions = ['campania','molise','puglia','basilicata','sicilia','sardegna'];
var domain = 'http://www.eventiesagre.it';
var basePath = 'http://www.eventiesagre.it/cerca/eventi/sagre/maggio/{{region}}/prov/cit/intit/rilib';

var result = 'path_to_folder{{region}}.json';

//start of scraper
function getData(path, region) {
    request(path, function (error, response, html) {
        if (!error && response.statusCode == 200) {
            var $ = cheerio.load(html);

// research information for each events
            var evt = {
                categoria: $('.category').text().trim().replace( /\s\s+/g, ' '),
                titolo: $('.summary').text(),
                sottotitolo: $('.titolo').siblings('.testoxxsmall').text(),
                dal: $('.dtstart').text(),
                al: $('.dtend').text(),
                tel: $('[alt="info evento"]').parent().next().text(),
            };

            var email = $('[src="/template/originalBlu/images/comuni/mail - at.gif"]').siblings('a').first();
            if (email.length) {
                evt.email = email.attr('href').split('mailto:')[1];
            }

            var adr = $('.location .adr ');
            adr.find('.testo10').remove();
            adr.find('.region').remove();

            evt.dove = adr.text().trim().replace( /\s\s+/g, ' ');

            var linkSito = $('[src="/template/originalBlu/images/comuni/sito - www.gif"]').siblings('a');
            if (linkSito.length > 1) {
                evt.sito = [];
                linkSito.each(function(i, sito){
                    evt.sito.push({
                        url: $(sito).attr('href'),
                        nome: $(sito).text()
                    });
                });
            } else {
                evt.sito = {
                    url: linkSito.attr('href'),
                    nome: linkSito.text()
                };
            }

            fs.appendFile(result.replace('{{region}}', region), JSON.stringify(evt) + '\n', function (err) {
                if (err) return console.log(err);
            });
            console.log(evt);
        }
    });
}


function getStuff(path, region) {
    request(path, function (error, response, html) {
        if (!error && response.statusCode == 200) {
            var $ = cheerio.load(html);
            $('.vevent').each(function(i, element){
                var link =  domain + $(element).find('.summary').attr('href');

                getData(link, region);

            });

            var next = $('.elencoNav a:contains(Avanti)').first().attr('href');

            if(next) {
                getStuff(next, region);
            }
        }
    });
}

regions.forEach(function(region){
    fs.writeFile(result.replace('{{region}}', region), '');

    getStuff(basePath.replace('{{region}}', region), region);
});

then i run the application and i get this error :

SyntaxError: Unexpected identifier
    at Object.exports.createScript (vm.js:44:10)
    at REPLServer.defaultEval (repl.js:117:23)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
    at REPLServer.Interface._ttyWrite (readline.js:830:14)
    at ReadStream.onkeypress (readline.js:109:10)

this error it's caused by the code on first raw var request = require('request');

4
  • Do you have packet.json file? Do you have node_modules folder? Commented May 9, 2016 at 8:19
  • I have node_modules folder,and i have a file called package.json not packet Commented May 9, 2016 at 8:23
  • Of course package.json! Can you get content of this file? Commented May 9, 2016 at 8:28
  • { "name": "eventiScraper", "version": "1.0.0", "description": "scraper for www.eventiesagre.it", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "devDependencies": { "cheerio": "^0.18.0", "request": "^2.53.0" } } Commented May 9, 2016 at 8:30

1 Answer 1

1

It seems that you have error on running your script. Try run node index.js. If your file name isn't index.js, then make changes.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.