1、安装python3.7
https://www.python.org/downloads/release/python-370/
选择了这个版本,直接默认下一步
2、安装pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
3、安装谷歌浏览器及谷歌驱动
谷歌浏览器
chrome69.0.3497.100
安装谷歌浏览器驱动
http://chromedriver.storage.googleapis.com/index.html
由于浏览器版本较高,这里选择了最新版本谷歌浏览器驱动
http://chromedriver.storage.googleapis.com/index.html?path=70.0.3538.16/
将驱动放在指定位置
mkdir /usr/local/src/chromedriver
解压后放入此文件夹
mv chromedriver /usr/local/src/chromedriver/
liugx@liugx chromedriver$ ./chromedriver -v
ChromeDriver 70.0.3538.16 (16ed95b41bb05e565b11fb66ac33c660b721f778)
4、安装 selenium
pip install selenium
简单 demo 如下
1、打开百度
2、搜索 site:zjj7.com
3、搜索结果中点击第一条搜索结果
4、关闭浏览器
from selenium import webdriver import time path = "/usr/local/src/chromedriver/chromedriver" driver = webdriver.Chrome(path) url = "https://www.baidu.com" driver.get(url) time.sleep(2) driver.find_element_by_id('kw').send_keys('site:zjj7.com') driver.find_element_by_id('su').click() time.sleep(2) result = driver.find_element_by_xpath("//div[@class='result c-container ']/h3/a") result.click() time.sleep(20) driver.close()