chrome命令行模式实现网页截图
curl https://intoli.com/install-google-chrome.sh | bash google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot
然而中文乱码, 就很难受. yum -y groupinstall Fonts 然后就好了!!! 有滚动条, 看着不美? 那设置一下分辨率. google-chrome-stable --force-device-scale-factor=1 --window-size=1920,1080 --no-sandbox --headless --disable-gpu --screenshot https://www.5566.net 有啥用? 将来可以输入一个网址, 自动生成这个网址的图片, 作为缩略图。 macbook: /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --force-device-scale-factor=1 --window-size=1920,1080 --no-sandbox --headless --disable-gpu --screenshot
python: os.system('/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --screenshot=./'+str(rowid)+'.png '+ url) 多进程走起: # !/usr/bin/python3 import pymysql import os from multiprocessing import Pool def getimg(row): (rowid, url) = row try: print(rowid, url) os.system( '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --headless --disable-gpu --screenshot=./screenshots/' + str( rowid) + '.png ' + url) except Exception as e: print(str(rowid) + " error") print(e) if __name__ == '__main__': # 打开数据库连接 db = pymysql.connect(host='', user='', port=, password='', database='') # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 查询语句 sql = "SELECT id, url FROM links order by id;" # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() db.close() pool = Pool(10) pool.map(getimg, results) pool.close() 如果不多进程: # !/usr/bin/python3 import pymysql import os from multiprocessing import Pool def getimg(rowid, url): print(rowid, url) os.system( 'chromium-browser --no-sandbox --headless --disable-gpu --screenshot=./screenshots/' + str( rowid) + '.png ' + url) print('shoot done') if __name__ == '__main__': db = pymysql.connect(host='127.0.0.1', user='root', port=3306, password='9e2c860bd894392e', database='linkace') cursor = db.cursor() sql = "SELECT id, url FROM links where thumbnail is null order by id DESC limit 10;" cursor.execute(sql) results = cursor.fetchall() for (rowid, url) in results: getimg(rowid, url) cursor.execute('update links set thumbnail=%s where id=%s;', ('/screenshots/' + str(rowid)+'.png', rowid)) db.commit() db.close() 后记: 对于新版本的chrome,设置windowsize并没有效果,不管我怎么设置都是固定的1600*1200像素。 /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --force-device-scale-factor=1 --window-size=1920,1080 --no-sandbox --headless --disable-gpu --screenshot https://www.baidu.com 针对此问题,我非常困惑,以前都可以的,越搞越倒退了? 现在至少都1920*1080的屏幕,截不全有个滚动条,很烦人,都看不到个全貌。 stack overflow上针对此问题,也有人问,看来不是我检查了一百遍的语法问题。
https://groups.google.com/g/chromedriver-users/c/Yro2uGBU1fg?pli=1
解决,换成谷歌浏览器的祖宗Chromium浏览器: /Applications/Chromium.app/Contents/MacOS/Chromium --force-device-scale-factor=1 --window-size=1920,1080 --no-sandbox --headless=old --disable-gpu --screenshot https://www.baidu.com centos命令行安装: yum install chromium chromium-browser --force-device-scale-factor=1 --window-size=1920,1080 --no-sandbox --headless --disable-gpu --screenshot=./screenshots/1.png http://www.nohup.net/