使用 Puppeteer 每页使用不同的代理 [英] Using different Proxies per page with Puppeteer

查看:87
本文介绍了使用 Puppeteer 每页使用不同的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Puppeteer 浏览器中为每页设置代理

Set Proxy per page in a Puppeteer Browser

使用 For of 循环为每个自动化实例创建一个新页面,但在两个页面加载并截取屏幕后,无论第一个开始自动化的实例是什么,它都会接管,并且只有自动化才会发生.

Using a For of loop to create a new page for each automated instance, but after both pages load and take a screenshot, whatever is the first instance to start automating first, it takes over and only that automation takes place.

根据我所见设置标志仅在创建新浏览器时才可行例如.

setting flags from what i've seen is only doable when creating a new browser eg.

  const browser = await puppeteer.launch({args:['--proxy-server=ip:port']});

似乎找不到任何关于通过页面设置它的文档.

cant seem to find any docs about setting it via the page.

推荐答案

我制作了一个模块来做到这一点.它称为 puppeteer-page-proxy.它支持为整个页面设置代理,或者如果您愿意,它可以为每个请求设置不同的代理.

I made a module that does this. It's called puppeteer-page-proxy. It supports setting a proxy for an entire page, or if you like, it can set a different proxy for each request.

首先安装它:

npm i puppeteer-page-proxy

然后要求它:

const useProxy = require('puppeteer-page-proxy');

使用起来很简单;为整个页面设置代理:

await useProxy(page, 'http://127.0.0.1:8000');

如果你想为每个请求使用不同的代理,那么你可以简单地这样做:

If you want a different proxy for each request,then you can simply do this:

await page.setRequestInterception(true);
page.on('request', req => {
    useProxy(req, 'socks5://127.0.0.1:9000');
});

那么如果你想确定你的页面的IP有没有变,你可以查一下;

Then if you want to be sure that your page's IP has changed, you can look it up;

const data = await useProxy.lookup(page);
console.log(data.ip);

它支持 httphttpssocks4socks5 代理,如果是,它还支持身份验证需要:

It supports http, https, socks4 and socks5 proxies, and it also supports authentication if that is needed:

const proxy = 'http://login:pass@127.0.0.1:8000'

存储库:https://github.com/Cuadrix/puppeteer-page-proxy

这篇关于使用 Puppeteer 每页使用不同的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆