使用selenium
+PhantomJS
动态抓取网页时,出现如下报错信息:
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
这个提示的意思就是:
selenium最新版本已经放弃PhantomJS,建议使用火狐或者谷歌无界面浏览器。
解决方案
降低Selenium版本
通过pip show selenium
显示,默认安装版本为3.8.1
。
将其卸载pip uninstall selenium
,重新安装并指定版本号 pip install selenium==2.48.0
。
再次运行,就不会提示这个信息了。
优化方案
使用Selenium+Chrome 或者 Selenium+Firefox 这里聊聊Selenium+Chrome的使用以及参数配置的情况
不带参数配置直接使用Selenium+Chrome比较简单:
from selenium import webdriver
# 启动浏览器
driver = webdriver.Chrome()
# 模拟请求
driver.get('http://www.baidu.com')
带参数配置:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 实例化一个启动参数对象
chrome_options = Options()
# 无界面运行(无窗口)
chrome_options.add_argument('--headless')
# 启动浏览器
driver = webdriver.Chrome(chrome_options=chrome_options)
# 请求百度首页
driver.get