Linux-Centos下selenium部署
阿新 • • 發佈:2020-07-29
1. 安裝chrome
1.1 新增repo
源
sudo vi /etc/yum.repos.d/google.repo
在開啟的空檔案中填入以下內容
[google] name=Google-x86_64 baseurl=http://dl.google.com/linux/rpm/stable/x86_64 enabled=1 gpgcheck=0 gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
1.2yum
安裝
sudo yum update
sudo yum install google-chrome-stable
1.3安裝必要的庫
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
2. 安裝chromedriver
2.1檢視chrome版本號
google-chrome --version
2.2 下載chromedriver
請注意chrome
和chromedriver
的區別,前者是瀏覽器,後者是其驅動,而二者缺一不可。https://www.cnblogs.com/zwnsyw/p/13387104.html有各個版本的chromedriver
,而你要選擇的版本要與你的chrome
版本對應
可先下載在windows在上傳linux
2.3 新增許可權(一定要新增,不然使用的時候會報錯)
chmod +x /usr/bin/chromedriver
3. 安裝selenium
pip 安裝selenium
pip install selenium
通過清華映象快速安裝 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ selenium pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ selenium
4.指令碼測試
將chromedriver放在Python指令碼同一目錄(記得新增許可權)fromselenium.webdriver import Chrome from selenium.webdriver.chrome.options import Options DRIVER_PATH = './chromedriver' if __name__ == "__main__": # 設定瀏覽器 options = Options() options.add_argument('--no-sandbox') options.add_argument('--headless') # 無頭引數 options.add_argument('--disable-gpu') # 啟動瀏覽器 driver = Chrome(executable_path=DRIVER_PATH, options=options) # 訪問目標URL driver.get('https://www.baidu.com/') print(driver.page_source) driver.save_screenshot("000.png") # 截圖 driver.close() driver.quit()
至此,恭喜你已經部署成功!