2

I am fetching data using PhantomJs Library

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('https://www.apwagner.com/appliance-part/wpl/wp661600', function(status) {
  if (status !== 'success') {
    console.log('Unable to access network');
  } else {
    var ua = page.evaluate(function() {
      return document.getElementById('ModelList').textContent;  
      //return document.getElementById('ModelList').innerHTML; 
    });
    console.log(ua);
  }
  phantom.exit();
});

Output

             1DNET3205TQ0
             7MMMS0100VW0
             7MMMS0100VW1
             7MMMS0120VM0
             7MMMS0140VW0
             7MMMS0160VW0

If I am trying to get output using innerHTML then output like

    <ul class="modelnos">
                        <li><a class="cursor" href="/appliance/1dnet3205tq0" onclick="return ProductService.SaveLogModelView('1DNET3205TQ0', 'MAC')"> 1DNET3205TQ0</a></li>
                        <li><a class="cursor" href="/appliance/7mmms0100vw0" onclick="return ProductService.SaveLogModelView('7MMMS0100VW0', 'MAC')"> 7MMMS0100VW0</a></li>
                        <li><a class="cursor" href="/appliance/7mmms0100vw1" onclick="return ProductService.SaveLogModelView('7MMMS0100VW1', 'MAC')"> 7MMMS0100VW1</a></li>
                        <li><a class="cursor" href="/appliance/7mmms0120vm0" onclick="return ProductService.SaveLogModelView('7MMMS0120VM0', 'MAC')"> 7MMMS0120VM0</a></li>
                        <li><a class="cursor" href="/appliance/7mmms0140vw0" onclick="return ProductService.SaveLogModelView('7MMMS0140VW0', 'MAC')"> 7MMMS0140VW0</a></li>
</ul>

But as the output in variable, I want this output in array format.

like var models = ["1DNET3205TQ0", "7MMMS0100VW0", "7MMMS0100VW1"];

and put this array in csv file.

How can I get this data in array and put in csv.

Updates :

Actually I want to create table html from each values in that array.

Like 3 columns in tables.

<table>
<tr><td> 1DNET3205TQ0 </td>
<td> 7MMMS0100VW0 </td>
<td> 7MMMS0100VW1 </td>
</tr>
<tr><td> 7MMMS0120VM0 </td>
<td> 7MMMS0140VW0 </td>
<td> 7MMMS0160VW0 </td>
</tr>
</table>
John
  • 177
  • 3
  • 13
  • 1
    Possible duplicate of [Is it possible to write data to file using only JavaScript?](http://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript) – Peon Dec 30 '16 at 10:01
  • For the first part, instead of `console.log(ua)` you can push values in an array. – abhishekkannojia Dec 30 '16 at 10:02
  • Try `split()` function to create array. `ua.split('\n')` or `ua.split(' ')` depending on the delimiter in the string. It will return an array – Anupam Dec 30 '16 at 10:05
  • how can I push this data into array. this data prints all at once – John Dec 30 '16 at 10:06
  • @abhishekkannojia anu Please checj updates in question – John Dec 30 '16 at 10:12

2 Answers2

0

A combination of using the split() function and the join() function can achieve the proper result.. (see comments inside of code for detailed explanations)

the comments are displayed in grey and have a /* and */ around them

/* create a new array for  ua values */

var my_array = [];

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('https://www.apwagner.com/appliance-part/wpl/wp661600', function(status) {
  if (status !== 'success') {
    console.log('Unable to access network');
  } else {
    var ua = page.evaluate(function() {
      return document.getElementById('ModelList').textContent;
      //return document.getElementById('ModelList').innerHTML; 
    });
    console.log(ua);

    /*  split    ua   to an array */
    my_array = ua.split(/\s+/);
  }
  phantom.exit();
});

/* now at the end to make into a 'csv' use join() */
/*  the ',' parameter specifies what character to put      in between the values */

var csv_array = my_array.join(",");
console.log(csv_array);

Steps to repdocuce:

  1. create a variable to store the values in with var my_array = [];

  2. In your function, use my_array.split(/\s+/); will split the result into an array by using a regular expression to match whitespace

  3. Once finished with the loop, use my_array.join(","); to combine all values into a single string with a "," in between each value

EDIT

To make the results into a table, instead of using my_array.join(), use a for loop, and each time the loop passes, you have to grab the array value with my_array[i] where i represents the index of the array, and then use document.createElement() to create the various table, tr, and td elements, and finally use appendChild() to insert the elements into the proper parent elements to create your table

In the example i added a destination div where the table will be added to as well

/* original array from other snippet */
var my_array = ["value 1", "value 2", "value 3"];

/* new table element */
var new_table = document.createElement("table");

/*  for loop which sets i to a value for each value listed in my_array */
for (var i = 0; i < my_array.length; i++) {

  /* create a row */
  var new_row = document.createElement("tr");

  /* create a cell for that row */
  var new_cell = document.createElement("td");

  /* set the text in the created cell */
  new_cell.innerText = my_array[i];

  /* add the cell to the row */
  new_row.appendChild(new_cell);

  /* add the row to the table */
  new_table.appendChild(new_row);

}
/* destination div */
var destination = document.getElementById("table_destination");

/* add the table to the destination div */
destination.appendChild(new_table);
#table_destination > table,
td {
  width: 50%;
  border: 1px solid black;
}
<div id="table_destination"></div>
mike510a
  • 2,102
  • 1
  • 11
  • 27
  • 1
    `push()` is used to put a single value in array, it wont create an array of values. You need `split()` instead – Anupam Dec 30 '16 at 10:13
  • I just updated my answer to take into account what @anu said – mike510a Dec 30 '16 at 10:18
  • Also, it doesnt necesaarily be a whitespace, it could be a newline as separator. So you need to try `split('\n')` if whitespace doesnt work. – Anupam Dec 30 '16 at 10:20
  • @anu actually `split(/\s+/)` matches newlines **and** whitespace http://stackoverflow.com/questions/25218677/javascript-split-function-to-split-into-array-at-new-line-or-white-space – mike510a Dec 30 '16 at 10:45
  • Great then my bad – Anupam Dec 30 '16 at 11:17
  • @anu Its an easy mistake to make -- regex (regular expressions) are seldom well-documented/consistent across platforms so in some languages, you are most likely correct that newlines would also have to be matched -- javascript fortunately treats any non-character space as whitespace – mike510a Dec 30 '16 at 11:20
0
var array = [];
var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('https://www.apwagner.com/appliance-part/wpl/wp661600', function(status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var ua = page.evaluate(function() {
            return document.getElementById('ModelList').textContent;
        });
        console.log(ua);
        array = ua.split(/\s+/);
    }
    phantom.exit();
});

var fs = require('fs');
//for users running with node
/*fs.writeFile("/home/data.csv", array.join(','), function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
});*/
//for users running with phantomjs
fs.write('/home/data.csv', array.join(','), 'w');
Cybersupernova
  • 1,833
  • 1
  • 20
  • 37
  • file is not created at location. getting error `TypeError: undefined is not a function (evaluating 'fs.writeFile')` – John Dec 30 '16 at 11:12
  • you are executing this with `node` or `phantomjs`. If `phantomjs` then please use `fs.write(path, content, 'w')` in place of `fs.writeFile` – Cybersupernova Dec 30 '16 at 11:33
  • can you please simple create an array of that variable `ua` so I can create table from that array. – John Dec 30 '16 at 11:46
  • u want to store in csv file or you want to create a html table? the variable `array` is containing all values in an array you can iterate on that – Cybersupernova Dec 30 '16 at 11:50
  • above solution not working like some encoding error while opening csv file. I want a html table format of that which can be put in csv file – John Dec 30 '16 at 11:59