栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

爬虫进阶-selenium模块

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬虫进阶-selenium模块

环境搭建:
  • selenium -> 自动化测试模块,非必要不建议使用该模块写爬虫
  1. 安装模块pip install selenium==3.3.1
  2. 下载浏览器对应的驱动
  3. 把解压出来的浏览器驱动可执行文件,移动到python解释器的所在文件夹
  4. 驱动可执行文件改名(selenium4.0之后的版本无需修改):
    • edge驱动:
      • 将msedgedriver.exe文件名改为MicrosoftWebDriver.exe
    • chrome驱动:
      • 将文件名的最后一位数字删除
基本使用:
  • 导入模块:
    • from selenium.webdriver import 浏览器导入指定浏览器模块
    • 或from selenium import webdriver导入全部浏览器模块
  • 创建浏览器对象:
    • web = 浏览器()
  • 打开一个网址:
    • web.get("xxx")
  • 利用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')
      • 提取标签内href对应的内容
  • 调用键盘按键:
    • 导入模块:from selenium.webdriver.common.keys import Keys
    • .send_keys("内容", Keys.具体键)
  • 鼠标点击事件:
    • x.click()
  • 基础案例:
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 = 浏览器()
  • 切换窗口:
    • 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")
  • 创建浏览器对象:
    • web = 浏览器(options=xxx)
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()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/656290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号