Linux_Puppeteer_Chrome 安装笔记

安装chrome浏览器

[visitor@localhost ~]$ vim 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

[visitor@localhost ~]$ sudo cp ~/google-chrome.repo /etc/yum.repos.d/

[visitor@localhost ~]$ sudo yum -y install google-chrome-stable --nogpgcheck

[visitor@localhost ~]$ google-chrome -version

Google Chrome 65.0.3325.181

启动浏览器

[visitor@localhost ~]$ google-chrome


安装nodejs和puppeteer

[visitor@localhost ~]$ curl --silent --location https://rpm.nodesource.com/setup_9.x | sudo bash -

[visitor@localhost ~]$ sudo yum -y install nodejs

[visitor@localhost ~]$ node -v

v9.11.1

[visitor@localhost ~]$ npm -v

5.6.0

[visitor@localhost ~]$ npm i --save puppeteer --ignore-scripts

[visitor@localhost ~]$ vim ~/example.js

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({executablePath: '/opt/google/chrome/chrome',
        headless: false, args: ['--no-sandbox', '--disable-setuid-sandbox']});
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

args参数详解

[visitor@localhost ~]$ node example.js


python版本的puppeteer叫pyppeteer,

pip install pyppeteer
pyppeteer-install #安装chromium

然后配置Python36\Lib\site-packages\pyppeteer\connection.py,第43行添加两个参数

self._ws = websockets.client.connect(self._url, max_size=None, loop=self._loop, ping_interval=None, ping_timeout=None)

解决报错: pyppeteer.errors.NetworkError: Protocol Error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.

你可能感兴趣的:(linux,chrome,puppeteer,centos,nodejs)