我想通过使用gimmeproxy.com免费API随机生成的代理服务器发出GET请求来抓取一些数据。
我能够获得代理ip / port并正在使用 ' HTTPS代理剂'使用代理数据设置代理。
每当我试着打电话给任何网站时,我总是得到
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>405 Method Not Allowed</title>
</head><body>
<h1>Method Not Allowed</h1>
<p>The requested method CONNECT is not allowed for the URL
/index.html.en.backup.</p>
</body></html>
这是我的节点脚本:
const request = require('request'), HttpsProxyAgent = require('https-proxy-agent');
generateRandomProxy(function(proxy){
var agent = new HttpsProxyAgent({
proxyHost: proxy.proxyHost,
proxyPort: proxy.proxyPort
});
request({
uri: "http://example.com",
method: "GET",
agent: agent,
timeout: 5000,
}, function(error, response, body) {
console.log(body);
});
})
function generateRandomProxy(cb){
request.get(' https://gimmeproxy.com/api/getProxy?get=true&cookies=true&country=US',{json:true},function(err,res){
if(!err){cb({
proxyHost: res.body.ip,
proxyPort: res.body.port
})}
else{console.log('problem obtaining proxy')}
})
}
&#13;
所以我的问题:如何通过代理路由我的请求,然后获得一个有效的返回正文?
如您所见,我不断获得 405 Method Not Allowed
感谢您的帮助。
答案 0 :(得分:0)
编辑:刚刚找到了Node.js的一些GimmeProxy包装器:gimmeproxy-request。
它声称当一方失败时会自动通过另一个代理重新路由请求。
使用此模块代码如下所示:
const setup = require('gimmeproxy-request').setup;
const request = require('gimmeproxy-request').request;
setup({
api_key: 'your api key',
query: 'get=true&cookies=true&country=US&supportsHttps=true&maxCheckPeriod=1800&minSpeed=10', // additional gimmeproxy query parameters
retries: 5, // max retries before fail
test: (body, response) => body.indexOf('captcha') === -1 && response.statusCode === 200 // test function
});
request('https://example.com', {
timeout: 10000 // additional request parameters, see https://github.com/request/request
},
function(err, res, body) {
console.log('err', err)
console.log('res', res)
console.log('body', body)
process.exit()
});
我想问题是你有时候不会从Gimmeproxy获得https代理,而'https-proxy-agent'只需要https代理。
要解决此问题,请使用同一作者的proxy-agent包并传递GimmeProxy响应的 curl 字段。它将选择正确的代理代理实现。
以下代码适用于我:
const request = require('request'), ProxyAgent = require('proxy-agent');
generateRandomProxy(function(proxy){
console.log(proxy);
var agent = new ProxyAgent(proxy.curl);
request({
uri: "https://example.com",
method: "GET",
agent: agent,
timeout: 5000,
}, function(error, response, body) {
console.log(error);
console.log(body);
});
})
function generateRandomProxy(cb){
request.get('https://gimmeproxy.com/api/getProxy?get=true&cookies=true&country=US&supportsHttps=true&maxCheckPeriod=1800&minSpeed=10',{json:true},function(err,res){
if(!err){cb(res.body)}
else{console.log('problem obtaining proxy')}
})
}
注意:如果您要调用https网站,则应使用 supportsHttps = true 参数查询支持https的代理。此外,使用 maxCheckPeriod = 1800 参数查询新代理是有意义的。设置 minSpeed = 10 也会有所帮助: