Wednesday, 12 April 2017

Node JS - pull VAR out of array

I trying to scrape data off a website. What is returned to me is an array that looks like.

var $ =  cheerio.load(body)

returns:

{ [Function]
  fn:
   { constructor: [Circular],
     _originalRoot:
      { type: 'root',
        name: 'root',
        attribs: {},
        children: [Object],
        next: null,
        prev: null,
        parent: null } },
  load: [Function],
  html: [Function],
  xml: [Function],
  text: [Function],
  parseHTML: [Function],
  root: [Function],
  contains: [Function],

The child object is what I'm after so...

var obj = $.root()[0]

returns:

{ type: 'root',
  name: 'root',
  attribs: {},
  children:
   [ { data: 'var usdata_real = {     // All_data node opens\n\n\tATL: {     // Country tag opens\n\t\tid: "ATL",\n\t\t\tdata: {     // Data node begins\n\t\t\tq1_1980: 169.304888373489,\n\t\t\tq2_1980: 164.585093140904,\n\t\t\tq3_1980: 166.167521028595,\n\t\t\tq4_1980: 163.
....

I want the 'data' in this array.

obj = obj['children'][0]['data']

returns:

var usdata_real = {     // All_data node opens

        ATL: {     // Country tag opens
                id: "ATL",
                        data: {     // Data node begins
                        q1_1980: 169.304888373489,
                        q2_1980: 164.585093140904,
                        q3_1980: 166.167521028595,
                        q4_1980: 163.040844286677,
                        q1_1981: 163.31019903107,
                        q2_1981: 162.449966027231,
                        q3_1981: 158.719442789479,
                        q4_1981: 162.744884934737,
.....

which is ALMOST perfect. Except it returns to me an array which is no bueno. I want to be able treat this as an object so i can do something like

obj['ATL'] 

and get they KEY/VALUE pairs.

So my question....

Is it possible to strip out the object in this array ('var usdata_real'). Because that is what I'm after.

Any tips would be greatly appreciated.

Thanks!



via Taylor White

No comments:

Post a Comment