当前位置:网站首页>爬虫——动作链、xpath、打码平台使用
爬虫——动作链、xpath、打码平台使用
2022-08-04 13:53:00 【山上有个车】
系列文章目录
第二章 代理搭建、爬取视频网站、爬取新闻、BeautifulSoup4介绍、bs4 遍历文档树、bs4搜索文档树、bs4使用选择器
第三章 selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
一、动作链
模拟按住鼠标拖动的效果,或者是在某个标签上的某个位置点击的效果,主要用来做验证码的破解(滑动验证码)
动作链事件表:
| 函数 | 作用 |
|---|---|
| click(on_element=None) | 单击鼠标左键 |
| click_and_hold(on_element=None) | 点击鼠标左键,不松开 |
| context_click(on_element=None) | 点击鼠标右键 |
| double_click(on_element=None) | 双击鼠标左键 |
| drag_and_drop(source, target) | 拖拽到某个元素然后松开 |
| drag_and_drop_by_offset(source, xoffset, yoffset) | 拖拽到某个坐标然后松开 |
| key_down(value, element=None) | 按下某个键盘上的键 |
| key_up(value, element=None) | 松开某个键 |
| move_by_offset(xoffset, yoffset) | 鼠标从当前位置移动到某个坐标 |
| move_to_element(to_element) | 鼠标移动到某个元素 |
| move_to_element_with_offset(to_element, xoffset, yoffset) | 移动到距某个元素(左上角坐标)多少距离的位置 |
| perform() | 执行链中的所有动作(一般所有动作排列好后才执行) |
| release(on_element=None) | 在某个元素位置松开鼠标左键 |
| send_keys(*keys_to_send) | 发送某个键到当前焦点的元素 |
| send_keys_to_element(element, *keys_to_send) | 发送某个键到指定元素 |
动作链的使用:
1.先拿到动作链对象(也就是标签)
2.对动作链对象设置动作事件(先设置的先执行)
3.动作设置完毕使用perform执行
# 动作链模块
from selenium.webdriver import ActionChains
模拟滑动验证码
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from selenium.webdriver.common.by import By
driver = webdriver.Edge()
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
driver.implicitly_wait(10) # 使用隐式等待
driver.maximize_window()
try:
driver.switch_to.frame('iframeResult') ##切换到iframeResult
sourse=driver.find_element(By.ID,'draggable')
target=driver.find_element(By.ID,'droppable')
#方式一:基于同一个动作链串行执行
# actions=ActionChains(driver) #拿到动作链对象
# actions.drag_and_drop(sourse,target) #把动作放到动作链中,准备串行执行
actions=ActionChains(driver).click_and_hold(sourse) # 对动作链对象使用click_and_hold为拖住sourse标签元素
actions.drag_and_drop_by_offset(target,10,20)
# drag_and_drop_by_offset为将actions对象拖到距离target对象的左侧10px、高20px的位置
actions.perform()
time.sleep(10)
finally:
driver.close()
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
from selenium.webdriver.common.by import By
driver = webdriver.Edge()
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
driver.implicitly_wait(10) # 使用隐式等待
driver.maximize_window()
try:
driver.switch_to.frame('iframeResult') ##切换到iframeResult
sourse=driver.find_element(By.ID,'draggable')
target=driver.find_element(By.ID,'droppable')
#方式二:不同的动作链,每次移动的位移都不同
ActionChains(driver).click_and_hold(sourse).perform()
distance = target.location['x'] - sourse.location['x'] # 两个控件之间的x轴的距离
track=0
while track < distance:
ActionChains(driver).move_by_offset(xoffset=20,yoffset=0).perform()# 移动量为xoffset
track+=20 # 此处只是控制循环条件
ActionChains(driver).release().perform()
time.sleep(10)
finally:
driver.close()
二、xpath
一般解析库都会有子的的搜索标签的方法,一般都会支持css和xpath
XPath 是一门在 XML 文档中查找信息的语言
| 符号 | 作用 |
|---|---|
| 标签名 | 找对应标签名的标签,如div、p、a等 |
| / | 找当前节点下的标签 |
| // | 找当前节点子子孙孙下的标签 |
| . | 表示当前节点 |
| … | 表示上一层 |
| @ | 表示取属性,如@id=‘xxx’、@href=‘www.baidu.com’ |
例如:
[@id="cnblogs_post_body"]/p[9]/strong
1.获取id为cnblogs_post_body的对象 [@id="cnblogs_post_body"]
2.当前路径下拿到第九个p标签 /p[9]
3.当前节点下的strong标签 /strong
三、打码平台使用
验证码的破解
简单的数字字母组合可以使用图像识别(python 现成模块),成功率不高
使用第三方打码平台(破解验证码平台),花钱,把验证码图片给它,它给你识别完,返回给你
例如:
超级鹰
边栏推荐
- Execution failed for task ‘:xxx:generateReleaseRFile‘.
- odoo13 note point
- Win11快速助手在哪里?Win11打开快速助手的方法
- 考研上岸又转行软件测试,从5k到13k完美逆袭,杭州校区小哥哥拒绝平庸终圆梦!
- eyb:JWT介绍
- Win11勒索软件防护怎么打开?Win11安全中心勒索软件防护如何设置
- k8s上安装mysql
- Is the code more messy?That's because you don't use Chain of Responsibility!
- LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
- 烂大街的缓存穿透、缓存击穿和缓存雪崩,你真的懂了?
猜你喜欢

职场漫谈:为什么越是内卷的行业越有人争着抢着往里冲?好奇怪的说...

考研上岸又转行软件测试,从5k到13k完美逆袭,杭州校区小哥哥拒绝平庸终圆梦!

eyb:JWT介绍

leetcode 48. Rotate Image 旋转图像(Medium)

量化细胞内的信息流:机器学习时代下的研究进展
![LeetCode 1403 非递增顺序的最小子序列[贪心] HERODING的LeetCode之路](/img/fd/c827608b96f678a67c7e920c51d8c5.png)
LeetCode 1403 非递增顺序的最小子序列[贪心] HERODING的LeetCode之路
MySQL性能指标TPS\QPS\IOPS如何压测?
![[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)](/img/9f/33e782b93fcaa15359450e59a7233d.png)
[Niu Ke brush questions-SQL big factory interview questions] NO5. Analysis of a treasure store (e-commerce model)

"Social Enterprises Conducting Civilian Personnel Training Specifications" group standard on the shelves of Xinhua Bookstore

【牛客刷题-SQL大厂面试真题】NO5.某宝店铺分析(电商模式)
随机推荐
文盘Rust -- 配置文件解析
"C pitfalls and pitfalls" reading summary
漏洞复现 - - - Alibaba Nacos权限认证绕过
LeetCode 1403 非递增顺序的最小子序列[贪心] HERODING的LeetCode之路
idea永久激活教程(新版)
化算力为战力:宁夏中卫的数字化转型启示录
如何查找endnote文献中pdf文件的位置
七夕邂逅爱,那人一定在
封装、继承、多态的联合使用实现不同等级学生分数信息的统计
内存定位利器-ASAN使用小结
企业应当实施的5个云安全管理策略
leetcode 48. Rotate Image (Medium)
LeetCode 1403 Minimum subsequence in non-increasing order [greedy] HERODING's LeetCode road
博途200/1500PLC多段曲线控温FB(支持40段控温曲线、段曲线搜索、暂停、跳段等功能)
17种正则表达式
TS---类型设置
关于redis的几件小事(五)redis保证高并发以及高可用
Win11勒索软件防护怎么打开?Win11安全中心勒索软件防护如何设置
面试官:如何查看/etc目录下包含abc字符串的文件?
postgre 支持 newsql 特性可行性有多大?