溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在puppeteer中使用代理

發(fā)布時間:2021-03-25 17:06:06 來源:億速云 閱讀:457 作者:Leah 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)如何在puppeteer中使用代理,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

背景

在使用puppeteer進行訪問頁面的時候,有時候需要用到代理。

環(huán)境: centos 7

chrome 安裝

puppeteer使用chrome。 參考chrome的命令行使用方式。 在官網(wǎng)中沒看到說明,不過一般的執(zhí)行文件都是支持help的。
服務器上安裝chrome :

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

添加內(nèi)容

[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

復制代碼 代碼如下:

google-chrome --headless --disable-gpu --proxy-server="socks5://127.0.0.1:1088" --print-to-pdf https://www.chromestatus.com/

上面的示例中并沒有 需要密碼(用戶)驗證的。

puppeteer 添加代理

在以上命令行中, 代理 option 添加方式是:

--proxy-server="socks5://127.0.0.1:1088"

在 puppeteer 中,啟動瀏覽器時添加代理:

復制代碼 代碼如下:

const browser = await puppeteer.launch({headless:true, args:['--no-sandbox','--proxy-server=socks5://127.0.0.1:1088']});

以上就是如何在puppeteer中使用代理,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI