Eu tentei uma abordagem totalmente diferente é usar o Scrapy, no entanto, ele tem o mesmo problema! Eis como resolvi o problema: SO: Python Scrapy - Filtro baseado no tipo MIME para evitar downloads de arquivos sem texto?
The solution is to setup a
Node.js
proxy and configure Scrapy to use it throughhttp_proxy
environment variable.What the proxy should do is:
- Take HTTP requests from Scrapy and sends it to the server being crawled. Then it gives back the response from to Scrapy i.e. intercept all HTTP traffic.
- For binary files (based on a heuristic you implement) it sends
403 Forbidden
error to Scrapy and immediate closes the request/response. This helps to save time, traffic and Scrapy won't crash.Sample Proxy Code That actually works!
http.createServer(function(clientReq, clientRes) {
var options = {
host: clientReq.headers['host'],
port: 80,
path: clientReq.url,
method: clientReq.method,
headers: clientReq.headers
};
var fullUrl = clientReq.headers['host'] + clientReq.url;
var proxyReq = http.request(options, function(proxyRes) {
var contentType = proxyRes.headers['content-type'] || '';
if (!contentType.startsWith('text/')) {
proxyRes.destroy();
var httpForbidden = 403;
clientRes.writeHead(httpForbidden);
clientRes.write('Binary download is disabled.');
clientRes.end();
}
clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(clientRes);
});
proxyReq.on('error', function(e) {
console.log('problem with clientReq: ' + e.message);
});
proxyReq.end();
}).listen(8080);