每次提到Chromium或者Chrome命令行,大家首先想到的就是Chromium Headless配合Selenium或Puppeteer开发动态爬虫。实际上大多数需要动态渲染的网站只是需要动态渲染,并不需要Selenium或者Puppeteer框架进行交互的。另外,有一些网站对于Selenium或者Puppeteer有检测措施,对于使用这些框架的浏览器会进行反爬取限制。本文主要讨论如何在Chromium在命令行中运行以及Chromium的各种参数。
首先让我们看以下命令:
chromium-browser --no-sandbox --disable-gpu --window-size=1024,2048 --headless=new --timeout=5000 --virtual-time-budget=30000 --user-agent="Mozilla/5.0 (Macintosh\\; Intel Mac OS X 10.15\\; rv:121.0) Gecko/20100101 Firefox/121.0" --screenshot https://icp.chinaz.com/sina.com.cn
不同平台上启动chromium的命令不同,在CentOS中使用chromium-browser,在MacOS中使用chromium。其他参数在大多可以在这里找到。其他没有涉及到参数可以百度或者谷歌查询看看。
远程连接到MacOS中,使用非无头模式启动插件会报错:
Check failed: x11::Connection::Get()->Ready(). Missing X server or $DISPLAY
因为上远程SSH并且没有开启display forward,所以没有显示设备。在搜索过程中也发现CentOS 7.5中也会有这样的问题。如:https://stackoverflow.com/questions/60304251/unable-to-open-x-display-when-trying-to-run-google-chrome-on-centos-rhel-7-5
MacOS中启动无头模式加载插件
命令行启动Chromium无头模式,加载插件。
chromium --headless=new --no-sandbox --disable-gpu --window-size=1024,2048 --remote-debugging-port=9090 --load-extension="./"
CentOS 7.9测试
在CentOS 7.9上测试Chromium无头模式加载插件没有问题,如下:
chromium-browser --headless=new --no-sandbox --disable-gpu --window-size=1024,2048 --remote-debugging-port=9090 --load-extension="./"

扫码联系船长