本文基於linux centos系統下操作;
一、使用yum安裝chrome
1.配置yum源
在目錄 /etc/yum.repos.d/ 下新建文件 google-chrome.repo
執行命令:
cd /ect/yum.repos.d/ vim google-chrome.repo
編輯文件寫入下面內容
[google-chrome] name=google-chrome baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch enabled=1 gpgcheck=1 gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
2.執行命令安裝
yum -y install google-chrome-stable
如果上面的出錯就執行下面這條,上面的Google官方源可能在中國無法使用
yum -y install google-chrome-stable --nogpgcheck
執行google-chrome-stable --version 查看版本號;輸出就安裝成功了
二、安裝selenium
執行命令
pip install selenium
可以用pip list列出信息看看安裝成功沒有
三、安裝驅動chromedriver
下載地址:http://chromedriver.storage.googleapis.com/index.html
注意你的驅動版本要適配chrome的版本
1.先查看chrome版本
控制台執行命令
google-chrome-stable --version
輸出:Google Chrome 79.0.3945.88
說明我的版本是79的,下載的驅動也要適配,去下載地址找79的,然后打開下面的notes.txt文件
可以看到驅動支持的chrome版本是79,沒問題了;
執行以下命令:
wget -N http://chromedriver.storage.googleapis.com/79.0.3945.36/chromedriver_linux64.zip #注意你的版本 unzip chromedriver_linux64.zip chmod +x chromedriver mv -f chromedriver /usr/local/share/chromedriver ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
四、測試
#!/usr/bin/python # -*- coding=utf-8 -*- from selenium import webdriver chromeOptions = webdriver.ChromeOptions() #chromeOptions.add_argument('--proxy-server=http://ip:port') #設置無賬號密碼的代理 #chromeOptions.add_argument('--disable-infobars') # 禁止策略化 chromeOptions.add_argument('--no-sandbox') # 解決DevToolsActivePort文件不存在的報錯 #chromeOptions.add_argument('window-size=1920x3000') # 指定瀏覽器分辨率 #chromeOptions.add_argument('--disable-gpu') # 谷歌文檔提到需要加上這個屬性來規避bug #chromeOptions.add_argument('--incognito') # 隱身模式(無痕模式) #chromeOptions.add_argument('--disable-javascript') # 禁用javascript #chromeOptions.add_argument('--start-maximized') # 最大化運行(全屏窗口),不設置,取元素會報錯 #chromeOptions.add_argument('--disable-infobars') # 禁用瀏覽器正在被自動化程序控制的提示 #chromeOptions.add_argument('--hide-scrollbars') # 隱藏滾動條, 應對一些特殊頁面 #chromeOptions.add_argument('blink-settings=imagesEnabled=false') # 不加載圖片, 提升速度 chromeOptions.add_argument('--headless') # 瀏覽器不提供可視化頁面. linux下如果系統不支持可視化不加這條會啟動失敗 driver = webdriver.Chrome(chrome_options=chromeOptions) driver.get('http://www.baidu.com') test = driver.find_element_by_id('u1') testa = test.find_elements_by_tag_name('a')[1].text print(testa) driver.quit() exit(0)
最后輸出 hao123 說明成功了;
第一次弄我遇到的報錯信息
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127
原因是chrome版本和chromedriver版本不對;
五、獲取js動態渲染的html 多用了個WebDriverWait顯示等待:詳情介紹可看https://blog.csdn.net/sinat_41774836/article/details/88965281
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC try: chromeOptions = webdriver.ChromeOptions() chromeOptions.add_argument('--no-sandbox') # 解決DevToolsActivePort文件不存在的報錯 chromeOptions.add_argument('--headless') # 瀏覽器不提供可視化頁面. linux下如果系統不支持可視化不加這條會啟動失敗 driver = webdriver.Chrome(chrome_options=chromeOptions) driver.get(URL) print('開始等待js動態渲染加載出節點,設置15秒超時') WebDriverWait(driver, 15).until( #presence_of_element_located 判斷某個元素是否被加到了 dom 樹里,並不代表該元素一定可見 EC.presence_of_element_located((By.CLASS_NAME, "listBox")) ) print('結束等待,繼續執行') #rhtml = driver.find_element_by_xpath("//*").get_attribute("outerHTML") rhtml = driver.page_source driver.quit() except:
#15秒內listBox元素還沒渲染出來,拋出異常; print('except')