Reputation: 79
I am trying to scrape a website. For that i am using casperjs and phantomjs. Earlier the code was working fine but now it is throwing an error on calling. I doubt if there is any error in code. Even on calling phantomjs in cmd it is throwing the same error.
I am enclosing code as well as error. Code
/**
* Scrape data on http://www.moneycontrol.com/financials/afenterprises/profit-loss/AFE01
*
* Usage: $ casperjs scraper.js
*/
var casper = require("casper").create({
pageSettings: {
userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"
}
});
var url = 'http://www.moneycontrol.com/financials/afenterprises/profit-loss/AFE01';
var currentPage = 1;
var jobs = [];
var terminate = function() {
this.echo("Exiting..").exit();
};
casper.start(url);
casper.waitForSelector('table#table4', processPage, terminate);
casper.run();
function getJobs() {
var rows = document.querySelectorAll('table#table4 tr[id^="table4"]');
var jobs = [];
for (var i = 0, row; row = rows[i]; i++) {
var a = row.cells[1].querySelector('a[href*="jobdetail.ftl?job="]');
var l = row.cells[2].querySelector('span');
var job = {};
job['title'] = a.innerText;
job['url'] = a.getAttribute('href');
job['location'] = l.innerText;
jobs.push(job);
}
return jobs;
}
if (currentPage >= 3 || !this.exists("table#table4")) {
return terminate.call(casper);
}
function getSelectedPage() {
var el = document.querySelector('li[class="navigation-link-disabled"]');
return parseInt(el.textContent);
}
var processPage = function() {
// Part 1: Scrape and print the jobs in the jobs table
jobs = this.evaluate(getJobs);
require('utils').dump(jobs);
// Part 2: Exit if we're finished scraping
if (currentPage >= 3 || !this.exists("table#table4")) {
return terminate.call(casper);
}
// Part 3: Click the Next link and wait for the next page
// of jobs to load
currentPage++;
this.thenClick("div#PB20 a#prevnext").then(function() {
this.waitFor(function() {
return currentPage === this.evaluate(getSelectedPage);
}, processPage, terminate);
});
};
Error
C:\Users\Vishu>phantomjs
internal/child_process.js:274
var err = this._handle.spawn(options);
^
TypeError: Bad argument
at TypeError (native)
at ChildProcess.spawn (internal/child_process.js:274:26)
at exports.spawn (child_process.js:339:9)
at Object.<anonymous> (C:\Users\Vishu\AppData\Roaming\npm\node_modules\phan
omjs\bin\phantomjs:22:10)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
Upvotes: 3
Views: 1232
Reputation: 736
Had a same error on my Mac, the solution was:
sudo npm uninstall -g phantomjs casperjs
Then manually download PhantomJS
and CasperJS
. And manually add to $PATH
their bin/
path installed.
Have fun.
Upvotes: 1