详解puppeteer使用代理

2020-06-17 06:12:12易采站长站整理

背景

在使用puppeteer进行访问页面的时候,有时候需要用到代理。

环境: centos 7

chrome 安装

puppeteer使用chrome。 参考chrome的命令行使用方式。 在官网中没看到说明,不过一般的执行文件都是支持help的。
在服务器上安装chrome :


vim /etc/yum.repos.d/google-chrome.repo

添加内容


[google-chrome]name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

安装浏览器


yum -y install google-chrome-stable

说明: yum search chrome 可以查看供安装的包

chrome 命令行代理

在shell中执行命令:


google-chrome -h

即可看到所有的命令选项,其中 OPTION 下面有代理添加的介绍以及示例:


--proxy-server=host:port
Specify the HTTP/SOCKS4/SOCKS5 proxy server to use for requests.
This overrides any environment variables or settings picked via
the options dialog. An individual proxy server is specified
using the format:

[<proxy-scheme>://]<proxy-host>[:<proxy-port>]

Where <proxy-scheme> is the protocol of the proxy server, and is
one of:

"http", "socks", "socks4", "socks5".

If the <proxy-scheme> is omitted, it defaults to "http". Also
note that "socks" is equivalent to "socks5".

Examples:

--proxy-server="foopy:99"
Use the HTTP proxy "foopy:99" to load all URLs.

--proxy-server="socks://foobar:1080"
Use the SOCKS v5 proxy "foobar:1080" to load all URLs.

--proxy-server="socks4://foobar:1080"
Use the SOCKS v4 proxy "foobar:1080" to load all URLs.

--proxy-server="socks5://foobar:66"
Use the SOCKS v5 proxy "foobar:66" to load all URLs.

It is also possible to specify a separate proxy server for dif‐
ferent URL types, by prefixing the proxy server specifier with a
URL specifier:

Example:

--proxy-server="https=proxy1:80;http=socks4://baz:1080"
Load https://* URLs using the HTTP proxy "proxy1:80". And
load http://*
URLs using the SOCKS v4 proxy "baz:1080".

--no-proxy-server
Disables the proxy server. Overrides any environment variables
or settings picked via the options dialog.

--proxy-auto-detect
Autodetect proxy configuration. Overrides any environment vari‐
ables or settings picked via the options dialog.

--proxy-pac-url=URL
Specify proxy autoconfiguration URL. Overrides any environment