在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"]')

 

 

posted @ 2020-05-31 18:17  凯帅  阅读(3722)  评论(1编辑  收藏  举报