这不是 Selenium 解决方案,但它可以很好地与 python 和 PhantomJS 一起工作。
这个想法是与 Chrome 开发者工具中的“网络”选项卡中的完全相同。为此,我们必须听取网页发出的每个请求。
Javascript/Phantomjs 部分
使用 phantomjs,这可以使用这个脚本来完成,在你自己方便的时候使用它:
// getResources.js
// Usage:
// ./phantomjs --ssl-protocol=any --web-security=false getResources.js your_url
// the ssl-protocol and web-security flags are added to dismiss SSL errors
var page = require('webpage').create();
var system = require('system');
var urls = Array();
// function to check if the requested resource is an image
function isImg(url) {
var acceptedExts = ['jpg', 'jpeg', 'png'];
var baseUrl = url.split('?')[0];
var ext = baseUrl.split('.').pop().toLowerCase();
if (acceptedExts.indexOf(ext) > -1) {
return true;
} else {
return false;
}
}
// function to check if an url has a given extension
function isExt(url, ext) {
var baseUrl = url.split('?')[0];
var fileExt = baseUrl.split('.').pop().toLowerCase();
if (ext == fileExt) {
return true;
} else {
return false;
}
}
// Listen for all requests made by the webpage,
// (like the 'Network' tab of Chrome developper tools)
// and add them to an array
page.onResourceRequested = function(request, networkRequest) {
// If the requested url if the one of the webpage, do nothing
// to allow other ressource requests
if (system.args[1] == request.url) {
return;
} else if (isImg(request.url) || isExt(request.url, 'js') || isExt(request.url, 'css')) {
// The url is an image, css or js file
// add it to the array
urls.push(request.url)
// abort the request for a better response time
// can be omitted for collecting asynchronous loaded files
networkRequest.abort();
}
};
// When all requests are made, output the array to the console
page.onLoadFinished = function(status) {
console.log(JSON.stringify(urls));
phantom.exit();
};
// If an error occur, dismiss it
page.onResourceError = function(){
return false;
}
page.onError = function(){
return false;
}
// Open the web page
page.open(system.args[1]);
Python部分
现在在 python 中调用代码:
from subprocess import check_output
import json
out = check_output(['./phantomjs', '--ssl-protocol=any', \
'--web-security=false', 'getResources.js', your_url])
data = json.loads(out)
希望这可以帮助