gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:29:36 25 4
gpt4 key购买 nike

我正在尝试抓取一个网站。为此,我使用 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)

最佳答案

我的 Mac 上也出现了同样的错误,解决方案是:sudo npm uninstall -g phantomjs casperjs

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

Have fun.

关于npm - PhantomJS 不再工作(通过 NPM 安装),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34293504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com