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

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

最佳答案

已更新,正在工作!

从 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。

您可以look at the PhantomJS docs

关于phantomjs - 如何控制 PhantomJS 跳过下载某种资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9486377/

相关文章:

javascript - CasperJS Scraper启动后立即退出

python - Firefox 在使用 Selenium-Webdriver 时看到 PhantomJS 看不到的元素

node.js - 使用 nodejs 从模板生成 PDF 的最佳方法

javascript - 在 phantomjs 上使用谷歌翻译

javascript - CasperJS 评估向下滚动后不执行

python - 在使用 Python 启动的脚本中运行 phantomjs/casperjs native 选项

continuous-integration - GitLab CI 无法安装/使用 PhantomJS

javascript - 如何每小时自动运行CasperJS脚本?

javascript - phantomjs pdf 到标准输出

javascript - 自动从网站按钮单击每日 csv 文件下载