ReferenceError: Cannot find variable: Buffer (iconv / phantomjs)

I made a cleanup program using phantomjs for a website that I often need to get information, and the problem was I would get these "Â" characters in all the cleaned data. Now if I write this data to a file, and in a separate node script, I use iconv to encode to ascii, and then use the regexp pattern to get rid of the resulting "?", This works great. But when I try to include the same exact function inside my cleanup program, I get this error:

ReferenceError: Can't find variable: Buffer

  C:/javascript/Phantom/node_modules/iconv-lite/encodings/internal.js:4
  C:/javascript/Phantom/node_modules/iconv-lite/encodings/internal.js:82
ReferenceError: Can't find variable: Buffer

  C:/javascript/Phantom/node_modules/iconv-lite/encodings/sbcs-codec.js:20
  C:/javascript/Phantom/node_modules/iconv-lite/lib/index.js:98 in getCodec
  C:/javascript/Phantom/node_modules/iconv-lite/lib/index.js:16 in encode
  siteScraper.js:72 in replaceDataAndWrite
  siteScraper.js:58

      

This is a cleanup program (with replaced url and privacy credentials):

var page = require('webpage').create();
var fs = require('fs');
var iconv = require('iconv-lite');

console.log('before page.open'),
    url = "xxxxxxxxxxxxxxxxxxxxxxxxx",
    url2 = "xxxxxxxxxxxxxxxxxxxxxxxx";

var credentials = {username: 'xxxxxxxxx', password: 'xxxxxxxx'}


page.open(url, function (status) {
     if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        console.log('inside page.open callback');
        page.evaluate(function (credentials) {
            document.querySelector('input[id=username]').value = credentials.username;
            document.querySelector('input[id=password]').value = credentials.password;
            document.querySelector('input[id=button_submit]').click();
            console.log('finished querying selectors');
        }, credentials);

        window.setTimeout(function () {
            page.render('postLogin.png');
            console.log('rendered post-login');

            page.open(url2, function (status) {
                if (status !== 'success') {
                console.log('Unable to access network');
                } else {
                    console.log('INSIDE frame');
                    window.setTimeout(function () {
                        page.render('framePic.png');
                        console.log('rendered framePic.png');
                            var output = page.evaluate(function () {
                                var output;
                                // get all table data
                                tables = document.getElementsByTagName('table');
                                // go through table data
                                for(i=0; i < tables.length; i++) {
                                    cells = tables[i].getElementsByTagName('td');
                                    // assign data cells to output var
                                    for (j=0; j < cells.length; j++) {
                                        output += cells[j].innerText + '\n';
                                    }
                                }
                                return output;
                            });
                        // get rid of /'s
                        while (output.indexOf("/") != -1)
                            {
                            replacedOutput = output.replace("/", "")
                            output = replacedOutput
                            }
                        // function call to encode output to ascii and replace a few things
                        replaceDataAndWrite(output);

                        // var path = 'scrapedData.txt';
                        // fs.write(path, output, 'w');
                        console.log('function complete');
                        phantom.exit();
                    }, 10000); // inner setTimeout
                } // second else
            });
        }, 10000); // outer setTimeout
    } // first else
}); // first page.open

function replaceDataAndWrite (data) {
        encodedData = iconv.encode(data, 'ascii');
        newData = encodedData.toString('ascii')
        replacedData = newData.replace(/\?/gi,"")
        // replacedData = replacedData.replace(/undefined/gi,"")
        fs.writeFile('scrapedData.txt', replacedData, function (err) {
            if (err) throw err;
        });
}

      

I literally just copied and pasted the code from the following iconvTest.js working program into the "replaceDataAndWrite ()" function in my scraping script (but obviously extracted a portion from the file I read):

var iconv = require('iconv-lite');
var fs = require('fs');

fs.readFile('scrapedData.txt', function(err, data) {
    if (err) throw err;
    encodedData = iconv.encode(data, 'ascii');

    newData = encodedData.toString('ascii')
    replacedData = newData.replace(/\?/gi,"")
    // replacedData = replacedData.replace(/undefined/gi,"")
    fs.writeFile('message.txt', replacedData, function (err) {
        if (err) throw err;
    });
});

      

Iconv works fine in this last example, but not in my cleanup program. Is this a bug in iconv or is there some other explanation?

+3


source to share





All Articles