前言
本来准备装Selenium+PhantomJS的,但是Selenium已经停止支持,果断更换Chrome.
/usr/local/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py:49: 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 '
安装Python3+Pip3
yum install -y python3-devel python3 python3 -V Python 3.6.8 pip3 -V pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
注:pip3已和python3捆绑安装
安装Selenium
pip3 install selenium
安装Chrome浏览器
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm yum localinstall google-chrome-stable_current_x86_64.rpm google-chrome --version Google Chrome 87.0.4280.88
安装chromedriver
wget http://npm.taobao.org/mirrors/chromedriver/87.0.4280.88/chromedriver_linux64.zip unzip chromedriver_linux64.zip mv chromedriver /usr/bin/
注:chromedriver要和浏览器版本一致
示例代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options def main(): chrome_options = Options() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--headless') chrome_options.add_argument('blink-settings=imagesEnabled=false') chrome_options.add_argument('--disable-gpu') driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', chrome_options=chrome_options) driver.get("https://gaojie.me") print(driver.page_source) driver.close() if __name__ == '__main__': main()
–no-sandbox:让Chrome在root权限下运行
–headless:浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
blink-settings=imagesEnabled=false:不加载图片, 提升速度
--disable-gpu:谷歌文档提到需要加上这个属性来规避bug
本文链接:https://jeff.xin/post/123.html
--EOF--
Comments
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。