环境搭建:
- selenium -> 自动化测试模块,非必要不建议使用该模块写爬虫
- 安装模块pip install selenium==3.3.1
- 下载浏览器对应的驱动
- 把解压出来的浏览器驱动可执行文件,移动到python解释器的所在文件夹
- 驱动可执行文件改名(selenium4.0之后的版本无需修改):
- edge驱动:
- 将msedgedriver.exe文件名改为MicrosoftWebDriver.exe
- chrome驱动:
基本使用:
- 导入模块:
- from selenium.webdriver import 浏览器导入指定浏览器模块
- 或from selenium import webdriver导入全部浏览器模块
- 创建浏览器对象:
- 打开一个网址:
- 利用xpath路径查到元素:
- x = web.find_element_by_xpath('')
- x = web.find_elements_by_xpath('')
- .text提取标签内的内容
- 利用标签名查找元素:
- find_element_by_tag_name('')
- find_elements_by_tag_name('')
- .text
- .get_attribute('href')
- 调用键盘按键:
- 导入模块:from selenium.webdriver.common.keys import Keys
- .send_keys("内容", Keys.具体键)
- 鼠标点击事件:
- 基础案例:
import time
from selenium.webdriver import Edge
from selenium.webdriver.common.keys import Keys
# 创建edge浏览器对象
web = Edge()
# 打开浏览器访问网址
web.get("https://www.lagou.com/")
# 提取`全国`标签的位置
el = web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a') # selenium4.0后废除该方法
# 鼠标点击时间 -- 点击el对象
el.click()
time.sleep(1)
# 找到文本框,输入指定的内容 -- 然后按回车
web.find_element_by_xpath('//*[@id="search_input"]').send_keys("python", Keys.ENTER)
# 找到元素的所在标签位置 -- 存在li_list列表内
li_list = web.find_elements_by_xpath('//*[@id="s_position_list"]/ul/li')
# 遍历li_list列表,并取出相应的内容
for li in li_list:
job_firm = li.find_elements_by_xpath('./div/div[2]/div/a')[0].text
job_name = li.find_elements_by_tag_name('h3')[0].text
job_money = li.find_elements_by_xpath('./div/div/div[2]/div/span')[0].text
print(job_firm, job_name, job_money)
窗口切换:
- 创建浏览器对象:
- 切换窗口:
- web.switch_to.window(web.window_handles[下标])
- 关闭子窗口:web.close()
- 处理iframe标签
- 先拿到iframe -> 切换视角到iframe -> 拿iframe内的数据
- xxx = web.find_element_by_xpath('')
- web.switch_to.frame(xxx)
- 切回原页面:web.switch_to.default_content()
- xxx = web.find_element_by_xpath('')
- 窗口切换案例:
from selenium.webdriver import Edge
from selenium.webdriver.common.keys import Keys
web = Edge()
web.get('https://www.zhipin.com/')
web.find_element_by_xpath('//*[@id="wrap"]/div[3]/div/div[1]/div[1]/form/div[2]/p/input').send_keys('python', Keys.ENTER)
web.find_element_by_xpath('//*[@id="main"]/div/div[3]/ul/li[1]').click()
web.switch_to.window(web.window_handles[-1])
cont = web.find_element_by_xpath('//*[@id="main"]/div[3]/div/div[2]/div[2]/div[1]/div').text
web.close()
web.switch_to.window(web.window_handles[0])
xinzi = web.find_element_by_xpath('//*[@id="main"]/div/div[3]/ul/li[1]/div/div[1]/div[1]/div/div[2]/span').text
gongsi = web.find_element_by_xpath('//*[@id="main"]/div/div[3]/ul/li[1]/div/div[1]/div[2]/div/h3/a').text
print("薪资:{0},公司:{1}n职业需求:{2}".format(xinzi, gongsi,cont))
web.close()
无头浏览器:
- 无头浏览器,顾名思义就是不显示浏览器窗口,让浏览器执行过程在后台自动完成
- 导入模块:from selenium.webdriver.浏览器.options import Options
- 配置无头浏览器参数:
- xxx = Options()
- xxx.add_argument("--headless")
- xxx.add_argument("--disable-gpu")
- 创建浏览器对象:
from selenium.webdriver import Edge
# 设置Edge无头浏览器模式
EDGE = {
"browserName": "MicrosoftEdge",
"version": "95.0.1020.40",
"platform": "WINDOWS",
"ms:edgeOptions": {
'extensions': [],
'args': [
'--headless',
'--disable-gpu'
]}
}
web = Edge(capabilities=EDGE)
web.get('https://www.maoyan.com/board')
# 找到元素的所在标签位置,并提取内容
dl = web.find_element_by_xpath('//*[@id="app"]/div/div/div/dl').text
print(dl)
处理验证码:
- 注册超级鹰验证码识别,调用该API接口
- 提取验证码图片:.screenshot_as_png
import time
import chaojiying
from selenium.webdriver import Edge
# 创建浏览器对象
web = Edge()
# 访问需要登陆的网站
web.get('http://www.chaojiying.com/user/login/')
time.sleep(2)
# 超级鹰账户信息
chaojiying = chaojiying.Chaojiying_Client('账号', '密码', '产品ID')
# 提取登陆时需要的验证码
img = web.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png
# 利用超级鹰接口识别
yzm = chaojiying.PostPic(img, 1902)["pic_str"]
# 获取账号输入位置,并输入账号
user = web.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys('账号')
# 获取密码输入位置,并输入密码
password = web.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys('密码')
# 获取验证码输入位置,并输入验证码
yanzheng = web.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(yzm)
# 获取登陆按钮的位置,并点登陆
web.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input').click()