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.2 yum
安裝
sudo yum update -y
sudo yum install google-chrome-stable -y
1.3安裝必要的庫
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts -y
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腳本同一目錄(記得添加權限)
from selenium.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()
至此,恭喜你已經部署成功!