环境:CentOS 7,无界面
安装 Chrome
1 2 3 4 5
| yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
google-chrome -version
|
1
| Google Chrome 83.0.4103.97
|
安装 chromedriver
chromedriver 淘宝镜像网站
1 2 3 4 5 6
| wget http://npm.taobao.org/mirrors/chromedriver/83.0.4103.39/chromedriver_linux64.zip
x chromedriver_linux64.zip mv ./chromedriver_linux64/chromedriver /usr/local/bin
|
安装虚拟桌面
因为是无界面系统,所以直接运行 selenium 是会报错的,需要先开启虚拟桌面。
1 2
| pip install pyvirtualdisplay yum install xorg-x11-server-Xvfb
|
尝试运行,会出现警告:
1 2 3
| from pyvirtualdisplay import Display display = Display()
|
1
| xdpyinfo was not found, X start can not be checked! Please install xdpyinfo!
|
忽略警告也能正常运行,为了消除警告:
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
from selenium import webdriver from pyvirtualdisplay import Display display = Display(visible=0, size=(1280, 720)) display.start()
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
prefs = {"profile.managed_default_content_settings.images":2} options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(options=options)
driver.set_window_size(1280, 720)
driver.get('https://www.baidu.com') print(driver.page_source)
selenium.quit() display.stop()
|
封装成类
如上操作,每次都需要关闭一系列资源的,可以使用 Python 的 with
进行封装。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| from selenium import webdriver from pyvirtualdisplay import Display import os
class WebDriver(object): def __init__(self): self.__os_name = os.name
def __enter__(self): if self.__os_name == 'nt': option = webdriver.ChromeOptions() self.__driver = webdriver.Chrome(executable_path='chromedriver', options=option) elif self.__os_name == 'posix': self.__display = Display(visible=0, size=(1280, 720)) self.__display.start() options = webdriver.ChromeOptions() options.add_argument('--no-sandbox') prefs = {"profile.managed_default_content_settings.images": 2} options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome(options=options) self.__driver.set_window_size(1280, 720) return self.__driver
def __exit__(self, exc_type, exc_val, exc_tb): self.__driver.quit() if self.__os_name == 'posix': self.__display.stop()
if __name__ == '__main__': from time import sleep with WebDriver() as d: d.get('https://www.baidu.com/s?wd=ip') sleep(5) sleep(5000)
|