PhantomJS:抓取多个URL时出现空白页面

时间:2012-07-06 20:39:53

标签: phantomjs

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

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

由于 罗希特夏尔

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

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();
}

2 个答案:

答案 0 :(得分:3)

有人在Google群组中回答这个问题。解决方案是在使用页面对象后调用page.release()。

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

答案 1 :(得分:0)

page.release()已在当前版本的PhantomJS(v1.9)中弃用。

现在应该使用page.close()来从内存中释放页面。

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

相关问题