1.安装相关库
pip3 install selenium
pip3 install pyvirtualdisplay
yum install Xvfb
yum install libXfont
2.安装chrome和配置chromedriver
# 下载chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate
# 安装chrome
yum install ./google-chrome-stable_current_x86_64.rpm
# 下载对应的chromedriver
wget http://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip
unzip chromedriver_linx64.zip
mv chromedriver /usr/bin/
3.代码示例
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800,600))
display.start()
options = webdriver.ChromeOptions()
options .add_argument('--headless')
options .add_argument('--no-sandbox')
options .add_argument('--disable-dev-shm-usage')
browser = webdriver.Chrome(chrome_options=options)
browser.get("http://www.baidu.com")
print(browser.page_source)
browser.quit()
driver.quit()