詳解puppeteer使用代理
背景
在使用puppeteer進行訪問頁面的時候,有時候需要用到代理。
環(huán)境: centos 7
chrome 安裝
puppeteer使用chrome。 參考chrome的命令行使用方式。 在官網中沒看到說明,不過一般的執(zhí)行文件都是支持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中執(zhí)行命令:
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
variables or settings picked via the options dialog.
其中 –proxy-server= 的值的格式是 [<proxy-scheme>://]<proxy-host>[:<proxy-port>]
其中 http 代理是不需要加 proxy-scheme ,以上的示例中顯示 socks 代理是需要添加scheme。
比如:
在本機建立一個 socks5 代理服務器, 本地端口是 1088
上面的示例中并沒有 需要密碼(用戶)驗證的。
puppeteer 添加代理
在以上命令行中, 代理 option 添加方式是:
--proxy-server="socks5://127.0.0.1:1088"
在 puppeteer 中,啟動瀏覽器時添加代理:
具體puppeteer使用參考網上實例。
說明
有關使用賬戶密碼credentials 的代理使用:
1 新建代理
可以在本地新建一個不用密碼的代理,該代理訪問有密碼的代理。 然后puppeteer 中使用該無密碼代理
2 頁面添加代理
具體使用可以參考: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageauthenticatecredentials
自己還沒使用過.
參考文章:
chrome 安裝 http://www.dhdzp.com/article/153596.htm
puppeteer api 說明 https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md
代理問題 https://github.com/GoogleChrome/puppeteer/issues/336
puppeteer 官方文檔 https://pptr.dev/#?product=Puppeteer&version=v1.5.0&show=api-class-page
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Node.JS循環(huán)刪除非空文件夾及子目錄下的所有文件
這篇文章主要介紹了Node.JS循環(huán)刪除非空文件夾及子目錄下的所有文件及node.js遞歸刪除非空文件夾的實例代碼,需要的朋友可以參考下2018-03-03
npm?ERR!Cannot?read?properties?of?null(reading?'p
這篇文章主要給大家介紹了關于npm?ERR!Cannot?read?properties?of?null(reading?'pickAlgorithm')報錯問題的解決方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-03-03

