Selenium無界面執行


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”參數是不用打開圖形界面


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM