chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式

WebDriverException: Message: unknown error: Chrome failed to start: crashed

1. 背景
在使用selenium + chrome浏览器渲染模式爬取数据时,如果并发任务过多,或者爬虫的运行时间很长,那么很容易出现浏览器崩溃的现象,如下:

这一般是资源消耗过大造成的(据说chrome浏览器有内存泄漏的情况)。那如何解决这个问题呢?

这种情况下,我们首先就会想到使用无界面的浏览器PhantomJS,但是PhantomJS现在年久失修,后继无人,对很多新的特性支持并不够好。不过好在Google今年在chrome中增加了一项新的特性: Headless mode ,这样我们就可以使用无界面的chrome来爬取数据了,占用资源更少,速度更快。而且可喜的是,google的团队承诺会一直维护它…
2. 环境
系统:win10
python 3.6.5
IDE:jupyter notebook
安装过chrome浏览器( 75.0.3770.100(正式版本) 64 位)
selenium 3.7.0
配置好ChromeDriver v2.46

注意:

因为Headless mode 是新推出的特性,只有高级的版本才能使用,并不向前兼容,所以对chrome浏览器和chromedriver的版本有要求:
1. 对chrome浏览器来说:
linux,unix系统需要 chrome浏览器 >= 59
Windows系统需要 chrome浏览器 >= 60
2. chromeDriver版本与chrome浏览器匹配:
这个部分参考文章:https://www.cnblogs.com/gaofighting/p/10757013.html

代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()
# 使用headless无界面浏览器模式
chrome_options.add_argument(‘--headless‘)
chrome_options.add_argument(‘--disable-gpu‘)

# 启动浏览器,获取网页源代码
browser = webdriver.Chrome(options=chrome_options)
mainUrl = "https://www.taobao.com/"
browser.get(mainUrl)
print(f"browser text = {browser.page_source}")browser.get_screenshot_as_file("test.png")#获取截图
browser.quit()

原文地址:https://www.cnblogs.com/gaofighting/p/11172956.html

时间: 2024-07-31 14:33:15

chrome浏览器爬虫WebDriverException解决采用python + selenium + chrome + headless模式的相关文章

Python selenium chrome 环境配置

Python selenium chrome 环境配置 一.参考文章: 1. 记录一下python easy_install和pip安装地址和方法 http://heipark.iteye.com/blog/1916758 2. selenium + python自动化测试环境搭建 http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html 3. Python-selenium-Firefox-chrome-IE问题解决方法 http:

python+selenium+chrome网页自动化

python+selenium+chrome网页自动化测试: 1.在pycharm中安装selenium:file-setting-project interpreter 中搜索selenium然后进行安装: 2.下载安装谷歌浏览器驱动:  引用某位同学的安装路径:https://www.cnblogs.com/qiezizi/p/8632058.html  很简单:下载完之后,我们将下载的文件放在 Python 的根目录下就可以了. 3.新建项目和.Py文件:举例:from selenium

在Centos7上安装Python+Selenium+Chrome+Chromedriver

1.下载Chrome 上一篇文章已经演示过了Python+Selenium+Firefox+Geckodriver安装步骤并通过自动化脚本打开百度 因此当前只需要安装Chrome和Chromedriver即可 官网下载地址:https://www.google.cn/chrome/(可以通过centos7自带火狐浏览器进行下载RPM包) 百度云下载:https://pan.baidu.com/s/1XLpKO-pIzxc0uw3h2u4YnQ(密码:0vu2) 百度云下载后通过Xftp软件传到C

基于Python+selenium+Chrome的网页自动化教程

Python版本:Python2.7 Selenium版本:selenium3 Chrome版本: 浏览器驱动(chromedriver)版本: Python的安装 Python下载链接:https://www.python.org/ python安装完成后 ctrl+R输入cmd进入命令行界面,输入Python再按回车,如果出现Python版本信息则安装成功 Python安装selenium分为在线安装和离线安装. 离线安装: Selenium下载链接:https://pan.baidu.co

Ubuntu安装Chrome浏览器,并解决Chrome浏览器无法启动问题

安装Chrome浏览器 #将下载源加入到系统的源列表 sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/apt/sources.list.d/ #导入谷歌软件的公钥,用于下面步骤中对下载软件进行验证 wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add - #对当前系统的可用更新列表进行更新 s

Chrome浏览器扩展开发系列之九:Chrome浏览器的chrome.alarms.* API

Chrome浏览器扩展程序通过chrome.alarms.* API,可以制定计划周期性地执行代码,或在指定时间执行代码. 要使用chrome.alarms.* API,首先需要在manifest.json文件中声明alarms授权如下: { "permissions": [ "alarms" ], } chrome.alarms.Alarm对象的属性如下: 属性名 类型 必选/可选 注释 name string 必选 alarm的名字 scheduledTime

【爬虫】如何用python+selenium网页爬虫

一.前提 爬虫网页(只是演示,切勿频繁请求):https://www.kaola.com/ 需要的知识:Python,selenium 库,PyQuery 参考网站:https://selenium-python-zh.readthedocs.io/en/latest/waits.html 二.简单的分析下网站 三.步骤 1.目标: 1.open brower 2.open url from selenium import webdriver from selenium.common.excep

python selenium Chrome 设置为手机模式

# -*- coding: utf-8 -*- from selenium import webdriver from time import sleep mobileEmulation = {'deviceName': 'Apple iPhone 4'} options = webdriver.ChromeOptions() options.add_experimental_option('mobileEmulation', mobileEmulation) driver = webdrive

python+selenium操作chrome浏览器抓取网页解决方案

以下操作均是在ubuntu系统下运行 from selenium import webdriver from scrapy.selector import Selector #操作chrome浏览器抓取淘宝 driver = webdriver.Chrome() driver.get('淘宝链接') print(driver.page_source) t_selector = Selector(text=driver.page_source) tm_price = t_selector.xpat