環(huán)境
- Docker
安裝
拉取PhantomJS(后面簡稱PJS)鏡像(第三方):
docker pull wernight/phantomjs:2
截圖代碼
打開https://baidu.com, 截圖并保存為test.png
screenshot.js
var page = require('webpage').create();
page.open('https://baidu.com', function () {
page.render('test.png');
phantom.exit();
});
文件夾準備
宿主機新建一個文件夾pjs
用于掛載到docker容器中, 將screeshot.js也存放至該文件夾.
在截圖成功后, 圖片會保存至該文件夾.
啟動容器
啟動容器, 掛載文件夾, 并進入bash終端:
docker run -v phantomjs:/data -it --rm wernight/phantomjs:2 /bin/bash
如果遇到權(quán)限不足的問題, 使用ROOT
用戶登錄即可:
docker run -v /root/tmp/phantomjs:/data --user root -it --rm wernight/phantomjs:2 /bin/bash
截圖
進入掛載的目錄, 運行截圖代碼即可:
cd /data
phantomjs screenshot.js