puppeteer使用代理

背景

在使用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
              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/master/docs/api.md#pageauthenticatecredentials

自己还没使用过.

参考文章:
chrome 安装 https://segmentfault.com/a/1190000007705458

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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nodejs使用puppeteer是通过调用puppeteer库来实现的。Puppeteer是一个基于Chrome浏览器内核的nodejs爬虫包,它可以模拟用户操作界面,包括页面滚动、按钮点击、导航跳转等功能。你可以通过以下步骤来实现nodejs使用puppeteer: 1. 首先,安装puppeteer库。你可以通过全局安装或者当前项目安装的方式进行安装。全局安装的命令是:npm -g install puppeteer;当前项目安装的命令是:npm -S install puppeteer。 2. 创建一个async函数,例如puppeteerTest,在函数内部进行puppeteer操作。 3. 在puppeteerTest函数中,你可以设置一些选项,例如设置用户代理(user agent),可以通过args参数来传递。例如,你可以设置options对象,其中headless属性用于控制是否以无头模式运行,args属性用于设置命令行参数,包括设置用户代理。 4. 使用require语句引入puppeteer库,并通过puppeteer.launch(options)方法来启动一个浏览器实例。 5. 调用browser.newPage()方法创建一个新的页面实例。 6. 可以使用page.setViewport()方法设置页面的视口大小。 7. 使用page.goto(url)方法跳转到指定的URL地址。 下面是一个示例代码: ```javascript const puppeteerTest = async () => { const user_agent = "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"; const options = { headless: false, // 是否以无头模式运行 args: ['--no-sandbox', user_agent] // 设置命令行参数,包括设置用户代理 }; const puppeteer = require('puppeteer'); const browser = await puppeteer.launch(options); const page = await browser.newPage(); await page.setViewport({ width: 1020, height: 900 }); // 设置页面视口大小 await page.goto("https://www.baidu.com"); // 跳转到指定的URL地址 }; puppeteerTest(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值