Joh*_*Dip 8 proxy node.js puppeteer
你能告诉我这是否可能吗?
我想使用多个代理并在代理拒绝连接时自动更改代理。
args: [
'--proxy-server=127.0.0.1:9876', // Or whatever the address is
]
Run Code Online (Sandbox Code Playgroud)
因此,有了这个,您可以使用一个代理,但是如何使用多个代理并让它在拒绝连接时自动更改?
Gra*_*ler 12
您可以安装tor
允许您通过 Tor 网络使用 Puppeteer 进行浏览的软件包,并且可以非常轻松地更改您的身份(IP 地址)。
使用以下--proxy-server
标志使用 Tor 启动 Puppeteer :
const browser = await puppeteer.launch({
args: [
'--proxy-server=socks5://127.0.0.1:9050',
],
});
Run Code Online (Sandbox Code Playgroud)
然后,在 上page.on('response')
,child_process.exec()
如果响应不成功,则使用 ( response.ok() === false
)更改代理。
以下命令将创建一个新的 Tor 身份:
(echo authenticate \'""\'; echo signal newnym; echo quit) | nc localhost 9051
Run Code Online (Sandbox Code Playgroud)
示例用法:
'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();
})();
Run Code Online (Sandbox Code Playgroud)
注意: Tor 可能需要一些时间来更改身份,因此在继续您的程序之前验证 IP 地址是否不同可能是一个很好的调用。