如何控制PhantomJS跳过下载某种资源? [英] How can I control PhantomJS to skip download some kind of resource?
本文介绍了如何控制PhantomJS跳过下载某种资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
phantomjs具有配置loadImage,
phantomjs has config loadImage,
但是我想要更多,
如何控制phantomjs跳过下载某种资源,
how can I control phantomjs to skip download some kind of resource,
例如CSS等...
=====
好消息: 此功能已添加.
good news: this feature is added.
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起,现有答案无效.您必须使用以下代码:
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()
}
};
如果使用abort()而不是cancel(),它将触发onResourceError.
If you use abort() instead of cancel(), it will trigger onResourceError.
这篇关于如何控制PhantomJS跳过下载某种资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文