PhantomJS不再工作(通过NPM安装)

时间:2015-12-15 15:48:14

标签: npm phantomjs casperjs

我正在试图抓一个网站。为此,我使用casperjs和phantomjs。早些时候代码工作正常,但现在它在调用时抛出错误。 我怀疑代码中是否有任何错误。即使在cmd中调用phantomjs,它也会抛出同样的错误。

我附上代码以及错误。 代码

 /**
* 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);
  });
  };

错误

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)

1 个答案:

答案 0 :(得分:1)

我的Mac上有同样的错误,解决方法是: sudo npm uninstall -g phantomjs casperjs

然后手动下载PhantomJSCasperJS。并手动添加到$PATH已安装的bin/路径。

  

玩得开心。