我正在尝试使用多个地址 ip 运行此代码,但我认为我将代理代码放在错误的位置有人可以帮忙吗,代理仪表板显示代码使用代理,但是当他打开浏览器时,地址 IP 没有改变仍然是我的本地 IP。代码需要运行多个浏览器,每个浏览器具有唯一的不同 IP。
const puppeteer = require("puppeteer-extra");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
puppeteer.use(StealthPlugin());
const { Cluster } = require("puppeteer-cluster");
const util = require('util');
const fs = require('fs');
const readFile = util.promisify(fs.readFile);
const langBtns = require('./langBtns');
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
let choices = Object.values(langBtns);
let randObj;
let buttons;
let lang;
let button;
let goodBtnSelector;
let cluster;
const start = async () => {
cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 4, // provide the puppeteer-core library
puppeteer,
puppeteerOptions: {
headless: false,
executablePath:
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
slowMo: 10,
defaultViewport: null,
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--no-first-run",
"--no-zygote",
"--disable-gpu",
"--disable-notifications",
"--proxy-server=megaproxy.rotating.proxyrack.net:1200"
],
},
// and provide executable path (in this case for a Chrome installation in Ubuntu)
});
await cluster.task(async ({ page, data: lang}) => {
await Promise.all([
await visitWebsite(page, lang),
]);
});
//add number of bots, here 5
for (let index = 0; index < 1000000; index++) {
//Choosing an object (lang+buttons) from the JSON file
randObj = choices[Math.floor(Math.random()*choices.length)];
//Storing the buttons of the choosing language in the array buttons
buttons = randObj.buttons;
button = buttons[Math.floor(Math.random()*buttons.length)];
lang = randObj.language;
goodBtnSelector = randObj.goodButtonSelector;
}
await cluster.idle();
await cluster.close();
};
const visitWebsite = async (page, lang) => {
await cluster.queue(lang);
// add proxy credentials
await page.authenticate({
username: 'XXXXXXXX',
password: 'XXXXXXXXXXX'
});
console.log(lang);
try{
await page.goto(`https://whatismyipaddress.com/`, {
waitUntil: "load",
timeout: 60000
});
await sleep(200 + Math.floor(Math.random() * 50));
let scriptFilePath = 'search.js';
let testScript = (await readFile(`${scriptFilePath}`));
await Promise.all([
eval('(async () => {' + testScript.toString() + '})')()
]);
}catch (e) {
console.log(e.toString());
}
};
start();