2

我编写了一个 PhantomJs 脚本,通过递归链接对 page.open() 的调用来抓取多个 URL。(下面的代码片段。)这适用于最多 3 或 4 个 URL,但是如果 URL 数量较多,我只会得到空白页。空白是指 document.URL 包含“about:blank”,并且屏幕截图仅显示空白的白色背景。我还注意到 phantomJs 的内存使用量不断增加,因为它继续处理大量 URL。我需要做些什么来取消分配用于呈现前一页的任何内存吗?

其他人见过这个问题吗?是否可以扩展 PhantomJs 以抓取更多的 URL(比如 100 个)?

谢谢罗希特

用于抓取多个 URL 的递归代码段:

srcProducts = [{'url':'http://...' }, { 'url': 'http://...' },...];
destProducts = [];
gRetries = 0;
process();

function process() {
  if (srcProducts.length == 0) {
    // Output to file
    phantom.exit();
  } else {
     product = srcProducts.pop();

     page = require('webpage').create();
     page.open(product['url'], onOpen);
  }
}

function onOpen(status) {
  // check status
  // scrape info into product

  destProducts.push(product);
  process();
}
4

2 回答 2

3

有人很友善地在谷歌群组上回答了这个问题。解决方案是在使用完页面对象后调用 page.release()。

https://groups.google.com/forum/?fromgroups#!topic/phantomjs/lquzLFvZtrA

于 2012-07-07T08:04:29.170 回答
0

page.release() has been deprecated in the current version of PhantomJS (v1.9).

You should now use page.close() instead to free the page from memory.

http://phantomjs.org/api/webpage/method/release.html http://phantomjs.org/api/webpage/method/close.html

于 2014-06-12T23:19:18.597 回答