javascript - Error with require node.js SyntaxError: Unexpected identifier -
i new node.js. created file named events_scraper.js , put code in 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 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 run application , error :
syntaxerror: unexpected identifier @ object.exports.createscript (vm.js:44:10) @ replserver.defaulteval (repl.js:117:23) @ bound (domain.js:254:14) @ replserver.runbound [as eval] (domain.js:267:12) @ replserver.<anonymous> (repl.js:279:12) @ replserver.emit (events.js:107:17) @ replserver.interface._online (readline.js:214:10) @ replserver.interface._line (readline.js:553:8) @ replserver.interface._ttywrite (readline.js:830:14) @ readstream.onkeypress (readline.js:109:10)
this error it's caused code on first raw var request = require('request');
it seems have error on running script. try run node index.js
. if file name isn't index.js
, make changes.
Comments
Post a Comment