1

I am trying to enter a set of asn prefix data to a database and I keep getting a postgres syntax error. This problem has completely stumped me. Here is the code where I pull a list of as numbers and pass them into an array. They are then fed into a loop that scrapes the website for the table that contains the prefix data. Every single As number works until number 81.

var request = require('request');
var cheerio = require('cheerio')
var pg = require('pg');
var squel=require('squel');
var conString="postgres://postgres:Password@localhost:5433/postgres"
var client = new pg.Client(conString);
var newArr = [];

client.connect(function (err,data){
    if(err) console.log("'Error connecting to PG'", err);
    else{
    var query = squel.select().field("asnumber").from('"asntable"').limit(130);
    // console.log(query.toString());
    client.query(query.toString(), function (err,res){
        if (err) throw err;
        var x = JSON.stringify((res.rows));
        var y = x.split(",");
            for (i in y){
            newArr.push(Number(y[i].replace(/\D/g,'')))
            }
         console.log(newArr);  

 for (apn in newArr) {
                var options = {
                    url : 'http://bgp.he.net/AS'+newArr[apn]+'#_prefixes',
                    headers:  {
                        'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
                    }
                };
                request(options, (function(apn) { return function(err, resp, body) {
                        var $ = cheerio.load(body);
                        var x = $('#table_prefixes4');
                        console.log("*"+x.text());
                            $('#table_prefixes4 tr').each(function(index, prefix) {
                            $(this).find('.nowrap').each(function(){
                            event = $(this).text().trim();
                            nextevent = $(this).next().text().trim();
                            console.log(apn+","+event+","+nextevent)
                            var intoquery = squel.insert().into("secondastable").set("asnumber",newArr[apn]).set("prefixes",event).set("description",nextevent);
                            console.log(intoquery.toString())
                            client.query(intoquery.toString());
                            });        
                            });
                }})(apn))  
            }

        });
   }
}); 

The problem is that I can find no discernible difference between the previous 0-80 and 81. Here is the error it gives me.

    error: syntax error at or near "s"
    at Connection.parseE (/Users/myname/Desktop/scraping/node_modules/pg/lib/connection.js:526:11)
    at Connection.parseMessage (/Users/myname/Desktop/scraping/node_modules/pg/lib/connection.js:356:17)
    at Socket.<anonymous> (/Users/myname/Desktop/scraping/node_modules/pg/lib/connection.js:105:22)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:748:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:410:10)
    at emitReadable (_stream_readable.js:406:5)
    at readableAddChunk (_stream_readable.js:168:9)
    at Socket.Readable.push (_stream_readable.js:130:10)

I've rewritten it below so you can set it without the database if you have postgres. Any leads would be greatly appreciated.

var request = require('request');
var cheerio = require('cheerio')
var pg = require('pg');
var squel=require('squel');
var conString="postgres://postgres:Password@localhost:5433/postgres"
var client = new pg.Client(conString);
var newArr = [];
a = [81]        
client.connect(function (err,data){
    if(err) console.log("'Error connecting to PG'", err);
    else{
            for (apn in a) {
                var options = {
                    url : 'http://bgp.he.net/AS'+a[apn]+'#_prefixes',
                    headers:  {
                        'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
                    }
                };
                request(options, (function(apn) { return function(err, resp, body) {
                        var $ = cheerio.load(body);
                        var x = $('#table_prefixes4');
                        console.log("*"+x.text());
                            $('#table_prefixes4 tr').each(function(index, prefix) {
                            $(this).find('.nowrap').each(function(){
                            event = $(this).text().trim();
                            nextevent = $(this).next().text().trim();
                            console.log(apn+","+event+","+nextevent)
                            var intoquery = squel.insert().into("secondastable").set("asnumber",a[apn]).set("prefixes",event).set("description",nextevent);
                            console.log(intoquery.toString())
                            client.query(intoquery.toString());
                            });        
                            });
                }})(apn))
            }
    }        
});
3
  • What in your code is triggering the error? Is that the entire error message? Commented Jul 23, 2014 at 0:48
  • Thats what I don't understand. Thats the entire error message. @muistooshort. Every asn I plug in works. If you take the url and put 81 where a[apn] is you can look at the asn table. The table is no different from any of the others. At least that I can find. Commented Jul 23, 2014 at 1:38
  • for (i in array) isn't the best way to iterate over an array, you're better off with for(i = 0; i < array.length; ++i) as for ... in is for iterating over an object's properties. Commented Jul 23, 2014 at 3:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.