当爬取带有iframe的网页数据时,如果直接用requests库去获取,返回结果是不包含iframe中内容的,网上了解可以用Selenium+webdriver来模拟浏览器访问爬取,于是在自己的虚拟机上安装了chrome和chromedriver。
1. 安装chrome
yum安装配置
-
创建yum源文件
cd /etc/yum.repo.d/ touch google-chrome.repo
-
配置yum源信息
[google-chrome] name=google-chrome baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch enabled=1 gpgcheck=1 gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
-
安装
yum -y install google-chrome-stable --nogpgcheck
-
完成后查看chrome版本
google-chrome --version
2. 安装chromedriver
下载地址:https://chromedriver.storage.googleapis.com/index.html
chromedriver和chrome是有对应关系的。
问题
当使用报如下错误:
File "/usr/local/python3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
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.)
(Driver info: chromedriver=70.0.3538.97 (d035916fe243477005bc95fe2a5778b8f20b6ae1),platform=Linux 3.10.0-862.14.4.el7.x86_64 x86_64)
百度解决办法为,把sandbox禁用。
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox') # 添加这句即可
driver = webdriver.Chrome('/usr/bin/chromedriver', chrome_options=chrome_options)