linux在单独使用pyecharts画图用snapshot-selenium 或者 snapshot-phantomjs中的make_snapshot截图保存的时候没有啥问题出现,
使用了selenium爬虫去截图的,用的是谷歌浏览器插件,单独运行没有问题
driver.find_element_by_xpath('/html/body/div[5]/section/div[10]/div[3]/table').screenshot("table.png")
设置好无界面参数和不需要root权限的参数就可以正常爬虫:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
但是呢,两个放在同一个进程当中,问题就出现了,先运行了爬虫函数后,再去调用make_snapshot截pyecharts画的图就会出现以下报错:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
后来发现其实make_snapshot截图的时候就是本身已经通过get_chrome_driver
函数将 driver
创建的代码封装起来了,所以,只好直接改他的源码了.。但是为啥单独运行的时候没问题一起运行就有bug我也没想明白,因机器而异。
所以只能通过改make_snapshot的源码,设置一下get_chrome_driver时候的参数就行了
操作方法如下:
1) 顺着代码往源头点开,或者在报错信息中直接点开报错的位置可以找到
我安装的位置是在: /opt/python3.6/lib/python3.6/site-packages/snapshot_selenium/snapshot.py
def get_chrome_driver():
options = webdriver.ChromeOptions()
options.add_argument("headless")
return webdriver.Chrome(options=options)
将上面加的参数添加到这里面就ok了
def get_chrome_driver():
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
return webdriver.Chrome(options=options)
2)另一种方式就是改成mock的形式:
from unittest import mock
def get_chrome_driver():
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
return webdriver.Chrome(options=options)
使用make_snapshot的时候重新获取谷歌插件就ok了~
with mock.patch('snapshot_selenium.snapshot.get_chrome_driver', get_chrome_driver):
# 需要安装 snapshot-selenium 或者 snapshot-phantomjs
make_snapshot(driver, bar_chart().render(), "bar.png")
重点是最顶部以及最底部的几行代码.
1)重新定义 get_chrome_driver
函数
2)通过mock方式替换 snapshot_selenium.snapshot.get_chrome_driver
函数.关键语句:
with mock.patch('snapshot_selenium.snapshot.get_chrome_driver', get_chrome_driver)