在Linux中使用selenium(環境部署)


 

 
1、安裝chrome
用下面的命令安裝Google Chrome
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
也可以先下載至本地,然后安裝
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install ./google-chrome-stable_current_x86_64.rpm
 
安裝必要的庫
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
 
2、安裝 chromedriver(末尾附chrome和chromedriver的對應版本)
 
淘寶源(推薦)
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip
 
將下載的文件解壓,放在如下位置
安裝unzip:yum install -y unzip zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
給予執行權限
chmod +x /usr/bin/chromedriver
 
3、運行代碼,查看是否成功(python下) linux格式必須是這樣
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options  # 使用無頭瀏覽器
from selenium.webdriver import ChromeOptions


chrome_options = Options()
chrome_options.add_argument("--headless")  # => 為Chrome配置無頭模式
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')

browser = Chrome(options=chrome_options)

browser.get(url='http://www.wanfangdata.com.cn/index.html')
browser.implicitly_wait(10)

search = input('請輸入關鍵詞:').strip()

query = browser.find_element_by_xpath('//*[@id="keyWords"]')

 

 


免責聲明!

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



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