timzaak / blog

8 stars 1 forks source link

Headless Chrome - Puppeteer #52

Closed timzaak closed 3 years ago

timzaak commented 4 years ago

最近因为爬虫业务原因,看了一下 headless chrome。简单总结一下

编程语言

Javascript,原因是 puppeteer 框架比较好,Java 的 Selenium 框架还是太笨重。

防侦测识别

下面这段代码,基本上可以作为 puppeteer 的 防侦测 Hello World!

const puppeteer = require('puppeteer-extra')
const StealthPlugin = require('puppeteer-extra-plugin-stealth');

//'--proxy-server=10.24.51.125:8411',

(async () => {
    puppeteer.use(StealthPlugin())
    const browser = await puppeteer.launch({
        headless: true,
        executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
        userDataDir: './tmp',

    })
    const page = await browser.newPage()
    await page.goto('https://bot.sannysoft.com')
    //await browser.close()
})()
{
  "name": "puppeteer-learn",
  "version": "1.0.0",
  "scripts": {},
  "dependencies": {
    "puppeteer-core": "2.0.0",
    "puppeteer-extra": "^3.1.7",
    "puppeteer-extra-plugin-stealth": "2.4.5"
  }
}

puppeteer-extra-plugin-stealth 会帮助自行解决防侦测识别。//据说 youtube 还是能识别。

IP 代理

代码上是 给 puppeteer.launch 添加--proxy-server=127.0.0.1:8411 参数。至于IP代理池,我自己基于 akka 写了个收集免费高匿名代理IP的服务,但是感觉还是用 tinyproxy + 云服务切换IP api 来做比较稳定一些。

常规爬取API/Demo

以上两个就足够用了

[WIP] Docker 部署 Ubuntu Desktop

用 这个版本的 Docker 主要是方便 Debug。等代码 stable 后,再考虑直接上 headless chrome。 docker 镜像:dorowu/ubuntu-desktop-lxde-vnc:bionic

等等要在此基础上封装一个带有 node + Chrome 浏览器的镜像出来。

至于 vnc 客户端,目前暂定的是:VNC Viewer for Mac。

Serve as a Service

估计是需要挂载一个 express 做成 web 服务了。可能还需要搭建一个任务调度系统,来做横向拓展

参考

timzaak commented 3 years ago

微软出了一款操作浏览器的框架: playwright