详解puppeteer使用代理

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

详解puppeteer使用代理

面向未来的历史   2020-05-16 我要评论

背景

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

环境: centos 7

chrome 安装

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

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

添加内容

[google-chrome]
name=google-chrome
baseurl=http:/https://img.qb5200.com/download-x/dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https:/https://img.qb5200.com/download-x/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
    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使用参考网上实例。

说明

有关使用账户密码credentials 的代理使用:

1 新建代理
可以在本地新建一个不用密码的代理,该代理访问有密码的代理。 然后puppeteer 中使用该无密码代理

2 页面添加代理
具体使用可以参考: https://github.com/GoogleChrome/puppeteer/blob/masterhttps://img.qb5200.com/download-x/docs/api.md

自己还没使用过.

参考文章:

chrome 安装 https:

puppeteer api 说明 https://github.com/GoogleChrome/puppeteer/blob/masterhttps://img.qb5200.com/download-x/docs/api.md

代理问题 https://github.com/GoogleChrome/puppeteer/issues/336

puppeteer 官方文档 http://pptr.dev/#?product=Puppeteer&version=v1.5.0&show=api-class-page

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们