更新时间:2023-11-26 21:18:46
我制作了一个模块来做到这一点.它称为 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);
它支持 http、https、socks4 和 socks5 代理,如果是,它还支持身份验证需要:
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'