简体   繁体   中英

Error with require node.js SyntaxError: Unexpected identifier

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');

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM