1、安裝Google-chrome(cent-os系統):
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install google-chrome-stable_current_x86_64.rpm
檢查是否安裝成功:google-chrome --version
2、安裝Webdriver驅動:
wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
添加到可執行文件命令里cp chromedriver /usr/bin/
檢查是否安裝成功:chromedriver --version
3、編寫腳本
from selenium import webdriver
def spider(url='https://baidu.com'):
option = webdriver.ChromeOptions()
option.add_argument('--headless')
option.add_argument('--disable-dev-shm-usage')
option.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path='chromedriver', chrome_options=option)
driver.get(url)
print(driver.page_source)
if __name__ == '__main__':
spider()
其中:
“–no-sandbox”參數是讓Chrome在root權限下跑
“–headless”參數是不用打開圖形界面