[英]how do I set a proxy server in my headless chrome request using Node JS Puppeteer
/ I am running a headless search request on chrome and i need to access a proxy server / /我在 chrome 上运行无头搜索请求,我需要访问代理服务器/
const puppeteer = require('puppeteer');
var url="https://www.google.com/search?q=";
var keyword="hotels";
var urls;
var desktopUserAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
const response=[];
var i=0;
var userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
(async () => {
const browser = await puppeteer.launch({headless: false });
const page = await browser.newPage();
urls=url+keyword;
page.setUserAgent(userAgent);
response[i]=await page.goto(urls);
console.log(await browser.version());
})();
//i need to able to access a proxy server in order to google search //我需要能够访问代理服务器才能进行谷歌搜索
You can pass in a proxy like this in an argument,您可以在参数中传入这样的代理,
const options = {
headless: false,
args: [
`--proxy-server=${proxyIP:proxyPORT}`,
`--ignore-certificate-errors`
]
};
const browser = await puppeteer.launch(options);
If you want proxy authentication, you can use the following in your page object,如果您想要代理身份验证,您可以在页面对象中使用以下内容,
await page.authenticate(user, pass);
I made a module that does this.我做了一个模块来做到这一点。 It's called puppeteer-page-proxy .
它被称为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.
它支持为整个页面设置代理,或者如果您愿意,它可以为每个请求设置不同的代理。
First install it:首先安装它:
npm i puppeteer-page-proxy
Then require it:然后要求它:
const useProxy = require('puppeteer-page-proxy');
Using it is easy;使用它很容易; Set proxy for an entire page:
为整个页面设置代理:
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');
});
Then if you want to be sure that your page's IP has changed, you can look it up;然后如果你想确定你的页面的IP有没有变,你可以查一下;
const data = await useProxy.lookup(page);
console.log(data.ip);
It supports http , https , socks4 and socks5 proxies, and it also supports authentication if that is needed:它支持http 、 https 、 socks4和socks5代理,如果需要,它还支持身份验证:
const proxy = 'http://login:pass@127.0.0.1:8000'
Repository: https://github.com/Cuadrix/puppeteer-page-proxy存储库: https : //github.com/Cuadrix/puppeteer-page-proxy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.