我计划在内部使用一个webservice,该服务使用一个参数,即URL,并返回表示该URL中解析的DOM的html。通过解析,我的意思是webservice将首先在该URL上获取页面,然后使用PhantomJS‘呈现’页面,然后在执行所有DHTML、AJAX调用等之后返回结果源。然而,在每次请求的基础上启动幻影(我现在正在做的)太慢了。我希望有一个PhantomJS实例池,其中一个实例总是可用的,以满足对我的webservice的最新调用。
以前做过这种事吗?我宁愿将这个work服务建立在他人的工作上,也不愿从头开始为自己编写一个池管理器/ http代理服务器。
More Context:我列出了下面已经看到的两个类似的项目,以及为什么我避免了每个项目,从而产生了一个关于管理一个PhantomJS实例池的问题。
jsdom --据我所见,它具有在页面上执行脚本的强大功能,但它并不试图复制浏览器行为,所以如果我使用它作为一个通用的"DOM解析器“,最终会有大量额外的编码来处理各种边缘情况、事件调用等。我看到的第一个例子是必须手动调用body标记的onload()函数,以便我使用节点来设置测试应用程序。这似乎是一个深兔子洞的开始。
Selenium -它只是有更多的移动部件,所以设置一个池来管理长期存在的浏览器实例比使用PhantomJS要复杂得多。我不需要任何它的宏记录/脚本的好处。我只想要一个were服务,它在获取网页和解析DOM方面具有同样的性能,就像我用浏览器浏览到那个URL一样(或者如果我可以让它忽略图像等等)。
发布于 2015-12-02 19:07:51
在我的硕士论文中,我开发了一个库幻影池,它就是这样做的。它允许提供作业,然后映射到PhantomJS工作人员。该库处理作业分配、通信、错误处理、日志记录、重新启动等。这个图书馆被成功地用来爬行100多万页。
示例:
下面的代码执行Google搜索0到9的数字,并将页面的截图保存为googleX.png。四个网站被并行爬行(由于创建了四个工人)。脚本是通过node master.js
启动的。
master.js (在Node.js环境中运行)
var Pool = require('phantomjs-pool').Pool;
var pool = new Pool({ // create a pool
numWorkers : 4, // with 4 workers
jobCallback : jobCallback,
workerFile : __dirname + '/worker.js', // location of the worker file
phantomjsBinary : __dirname + '/path/to/phantomjs_binary' // either provide the location of the binary or install phantomjs or phantomjs2 (via npm)
});
pool.start();
function jobCallback(job, worker, index) { // called to create a single job
if (index < 10) { // index is count up for each job automatically
job(index, function(err) { // create the job with index as data
console.log('DONE: ' + index); // log that the job was done
});
} else {
job(null); // no more jobs
}
}
worker.js (在PhantomJS环境中运行)
var webpage = require('webpage');
module.exports = function(data, done, worker) { // data provided by the master
var page = webpage.create();
// search for the given data (which contains the index number) and save a screenshot
page.open('https://www.google.com/search?q=' + data, function() {
page.render('google' + data + '.png');
done(); // signal that the job was executed
});
};
https://stackoverflow.com/questions/9961254
复制相似问题