How can I control PhantomJS to skip download some kind of resource?

后端 未结 4 2083
Happy的楠姐
Happy的楠姐 2020-12-13 03:59

phantomjs has config loadImage,

but I want more,

how can I control phantomjs to skip download some kind of resource,

such as css etc...

===

4条回答
  •  旧巷少年郎
    2020-12-13 04:06

    UPDATED, Working!

    Since PhantomJS 1.9, the existing answer didn't work. You must use this code:

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

    If you use abort() instead of cancel(), it will trigger onResourceError.

    You can look at the PhantomJS docs

提交回复
热议问题