我怎样才能控制PhantomJS跳过下载某种资源?[英] How can I control PhantomJS to skip download some kind of resource?

问题描述

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

推荐答案

已更新,工作中!

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

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

您可以查看 PhantomJS 文档

本文地址:https://www.itbaoku.cn/post/1739838.html