Installation notes of Ubuntu 18 simplest puppeter

Time:2020-5-9

Reference resources

  • QQ group – Javascript advanced crawler – author self built group, welcome to join!
  • Awesome Java crawler – Crawler related tools and data collected by the author
  • A script to help you automatically create an Alibaba cloud preemptive instance and enable network acceleration – automatically create an Alibaba cloud overseas preemptive node
  • Install node.js binary publishing package on CentOS – install the latest version of nodejs on CentOS
  • Notes for installing the simplest puppeter in CentOS 7 – you can see this article for installing the puppeter on CentOS

Preface

Previously, I tried to install puppeter on centos7 and enable it to start normally in headless mode and display Chinese font correctly.
Today, I tried to achieve the same goal on Ubuntu 18, so I will record it here so as not to forget it later.

step

  1. Install nodejs, the same as centos7, omitted
  2. Install cnpm, omitted
  3. Install the puppeter and use it directlycnpm install puppeteerthat will do
  4. According to the wiki article on the puppeter project home page, use the following command to install the missing libraries:

    apt-get install -y gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
  5. Chrome can start headless here, but the Chinese font is displayed as a box, because the Chinese font is not included in the image. At this time, just move a few common fonts from windows to Ubuntu:

    • Browse to Windows font directoryC:\Windows\Fonts
    • Upload several common fonts, such as “Song typeface regular”, “Microsoft YaHei”, to Ubuntu’s/usr/share/fontsDirectory, filename:simsun.ttc, msyh.ttc
    • Install several font toolssudo apt-get -y install fontconfig xfonts-utils
    • Create font index information, update font cache, and execute the following commands in turn:

      1. mkfontscale
      2. mkfontdir
      3. fc-cache
    • implementfc-list :lang=zhList the Chinese fonts of the system. At this time, you should see several Chinese fonts

At this point, the installation is complete and can be verified.

Validation (consistent with CentOS steps)

  • Write index.js as follows:

    const puppeteer = require('puppeteer');
    (async () => {
      const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
      const page = await browser.newPage();
      await page.goto('https://www.segmentfault.com');
      await page.screenshot({path: 'sf.png'});
      await browser.close();
    })();

    be careful:
    1) Must add--no-sandboxParameter to start chrome, otherwise an error will be reported
    2) The default is to start chrome in headless (no interface) mode, i.e. bring it with you--headlessparameter

  • node index.jsExecuting the above script will generate a screenshot of the home page under the current directory. You can open the screenshot file to see if Chinese can be displayed normally