gpt4 book ai didi

node.js - Puppeteer 使用多个代理并在代理拒绝连接时更改自动代理

转载 作者:搜寻专家 更新时间:2023-10-31 23:40:14 25 4
gpt4 key购买 nike

你能告诉我这是否可行吗?

我想使用多个代理并在代理拒绝连接时自动更改代理。

args: [
'--proxy-server=127.0.0.1:9876', // Or whatever the address is
]

因此,您可以使用一个代理,但如何使用多个并在拒绝连接时自动更改?

最佳答案

使用 Tor。

您可以 install the tor package这将允许您通过 Tor 网络使用 Puppeteer 浏览,并非常容易地更改您的身份(IP 地址)。

使用 --proxy-server 标志用 Tor 启动 Puppeteer:

const browser = await puppeteer.launch({
args: [
'--proxy-server=socks5://127.0.0.1:9050',
],
});

然后,在 page.on('response') 上, 使用 child_process.exec() 更改代理如果响应不成功 ( response.ok() === false )。

以下命令将创建一个新的 Tor 身份:

(echo authenticate \'""\'; echo signal newnym; echo quit) | nc localhost 9051

示例用法:

'use strict';

const puppeteer = require('puppeteer');
const exec = require('child_process').exec;

(async () => {
const browser = await puppeteer.launch({
args: [
'--proxy-server=socks5://127.0.0.1:9050'
],
});
const page = await browser.newPage();
let current_ip_address = '';

page.on('response', response => {
if (response.ok() === false) {
exec('(echo authenticate \'""\'; echo signal newnym; echo quit) | nc localhost 9051', (error, stdout, stderr) => {
if (stdout.match(/250/g).length === 3) {
console.log('Success: The IP Address has been changed.');
} else {
console.log('Error: A problem occured while attempting to change the IP Address.');
}
});
} else {
console.log('Success: The Page Response was successful (no need to change the IP Address).');
}
});

await page.goto('http://checkip.amazonaws.com/');

current_ip_address = await page.evaluate(() => document.body.textContent.trim());

console.log(current_ip_address);

await browser.close();
})();

Note: Tor may take a moment to change identities, so it might be a good call to verify that the IP Address is different before continuing with your program.

关于node.js - Puppeteer 使用多个代理并在代理拒绝连接时更改自动代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com