如何控制PhantomJS跳过下载某种资源?

ati*_*n25 52 phantomjs

phantomjs有配置loadImage,

但我想要更多,

如何控制phantomjs跳过下载某种资源,

比如css等......

=====

好消息:此功能已添加.

https://code.google.com/p/phantomjs/issues/detail?id=230

要旨:

page.onResourceRequested = function(requestData, request) {
    if ((/http:\/\/.+?\.css/gi).test(requestData['url']) || requestData['Content-Type'] == 'text/css') {
        console.log('The url of the request is matching. Aborting: ' + requestData['url']);
        request.abort();
    }
};
Run Code Online (Sandbox Code Playgroud)

web*_*o80 17

工作!

自PhantomJS 1.9以来,现有的答案都不起作用.您必须使用此代码:

var webPage = require('webpage');
var page = webPage.create();

page.onResourceRequested = function(requestData, networkRequest) {
  var match = requestData.url.match(/wordfamily.js/g);
  if (match != null) {
    console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
    networkRequest.cancel(); // or .abort() 
  }
};
Run Code Online (Sandbox Code Playgroud)

如果使用abort()而不是cancel(),它将触发onResourceError.

您可以查看PhantomJS文档


www*_*.tk 7

所以最后你可以尝试这个http://github.com/eugenehp/node-crawler

否则你仍然可以使用PhantomJS尝试以下方法

简单的方法是加载页面 - >解析页面 - >排除不需要的资源 - >将其加载到PhatomJS中.

另一种方法是简单地阻止防火墙中的主机.

(可选)您可以使用代理来阻止某些URL地址和查询.

另外一个,加载页面,然后删除不需要的资源,但我认为这不是正确的方法.


bai*_*ain 6

使用page.onResourceRequested,例如loadurlwithoutcss.js:

page.onResourceRequested = function(requestData, request) {
    if ((/http:\/\/.+?\.css/gi).test(requestData['url']) || 
            requestData.headers['Content-Type'] == 'text/css') {
        console.log('The url of the request is matching. Aborting: ' + requestData['url']);
        request.abort();
    }
};
Run Code Online (Sandbox Code Playgroud)