安装puppeteer,使用cnpm
解决依赖
$ yum -y update
$ yum install -y pango libXcomposite libXcursor libXdamage libXext libXi libXtst cups-libs libXScrnSaver libXrandr GConf2 alsa-lib atk gtk3 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
$ yum update nss -y
执行代码
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import * as pptr from 'puppeteer';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
async getHello() {
const browser = await pptr.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();
await page.goto("https://translate.google.cn");
return await page.title();
}
}