Answer the question
In order to leave comments, you need to log in
How to automatically change the proxy address?
Hey! Tell me pliz how to implement automatic change of proxy address on http request?
There is a list of proxy addresses in a text file and one address is randomly taken from it into a variable.
The bottom line is that now, if during the request there are any errors that are displayed in the console in the server response, or a non-working proxy comes across, the requests continue to go and the error may repeat. How to make it so that in such a situation a new proxy address is immediately substituted so that requests work normally?
Code below:
var proxyList = fs.readFileSync('proxy-list.txt').toString().split("\n");
var randomIp = Math.floor(Math.random() * proxyList.length);
var currentIp = proxyList[randomIp]
setInterval(function() {
Request.get({
url: 'https://www.example.com/',
json: true,
headers: {'User-Agent': 'request'},
proxy: 'http://' + currentIp // случайный proxy из массива
}, (err, res, body) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
console.log(body);
}
});
}, 2000);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question