Das Rotieren von Proxys ist eine gute Idee, da es hilft, eine Erkennung zu vermeiden und verhindert, dass Websites Ihre IP-Adresse blockieren. Websites können IP-Adressen nachverfolgen und blockieren, die innerhalb kurzer Zeit zu viele Anfragen stellen oder mit Scraping-Aktivitäten in Verbindung stehen.
Wir können die Website „Free Proxy List“ nutzen, um eine Liste mit Proxys zusammenzustellen, die wir bei jeder `axios`-Anfrage rotieren können.
Bitte beachte, dass die Liste der Proxys, die du auf der Website findest, von der Liste abweichen kann, die ich zusammengestellt habe. Die von mir erstellte Liste sieht wie folgt aus:
const proxiesList = [
{
protocol: 'http',
host: '217.6.28.219',
port: 80
},
{
protocol: 'http',
host: '103.21.244.152',
port: 80
},
{
protocol: 'http',
host: '45.131.4.28',
port: 80
}
];
Okay, fügen wir also die `proxy`-Eigenschaft wieder in die `axios`-Konfiguration ein. Aber anstatt nur einen Proxy zu verwenden, wählen wir zufällig einen aus unserer Proxy-Liste aus. Der Code sieht wie folgt aus:
res = await axios.get('https://api.ipify.org?format=json', {
proxy: proxiesList[Math.floor(Math.random() * proxiesList.length)]
})
Dies ist der Inhalt der Datei index.js:
// Import axios
const axios = require('axios');
const proxiesList = [
{
protocol: 'http',
host: '217.6.28.219',
port: 80
},
{
protocol: 'http',
host: '172.67.180.244',
port: 80
},
{
protocol: 'http',
host: '45.131.4.28',
port: 80
}
];
(async () => {
// For storing the response
let res
try {
// Make a GET request with Axios
res = await axios.get('https://api.ipify.org?format=json', {
proxy: proxiesList[Math.floor(Math.random() * proxiesList.length)]
})
// Log the response data
console.log(res.data)
} catch(err) {
// Log the error
console.log(err)
}
// Exit the process
process.exit()
})()