使用selenium和chrome driver去掉界面的方法

最近在用python写爬虫,刚开始使用的是PhantomJS,单线程下支持不错,但是在多线程下效果差,而且会存在一些内存泄露的问题,下图是stackoverflow上的一些评论:

image.png

由于考虑分布式,所以chrome效率会比PhantomJS高不少,但是chrome如果开界面的话太吃内存了。那么有什么办法能够是chrome不打开界面呢?
我在网上查了一种说法是用:Python的pyvirtualdisplay库就能引入虚拟界面。而我实际用了一下并不管用。最后Google了下找到一种新的办法:

chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('headless')
chromeOptions.add_argument('window-size=1200x600')
driver = webdriver.Chrome(chrome_options=chromeOptions)

搞定!
PS:headless会导致一些比如element not visble的问题,记得在getURL之后driver.set_window_size(1024, 768)

你可能感兴趣的:(使用selenium和chrome driver去掉界面的方法)