当前位置:网站首页>Crawler - action chain, xpath, coding platform use
Crawler - action chain, xpath, coding platform use
2022-08-04 14:04:00 【There is a car on the mountain】
系列文章目录
第二章 代理搭建、爬取视频网站、爬取新闻、BeautifulSoup4介绍、bs4 遍历文档树、bs4搜索文档树、bs4使用选择器
第三章 selenium基本使用、无界面浏览器、selenium的其他用法、selenium的cookie、爬虫案例
一、动作链
模拟按住鼠标拖动的效果,或者是在某个标签上的某个位置点击的效果,主要用来做验证码的破解(滑动验证码)
Action chain event table:
| 函数 | 作用 |
|---|---|
| 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() | 执行链中的所有动作(Generally, all actions are executed after they are arranged) |
| release(on_element=None) | 在某个元素位置松开鼠标左键 |
| send_keys(*keys_to_send) | 发送某个键到当前焦点的元素 |
| send_keys_to_element(element, *keys_to_send) | 发送某个键到指定元素 |
The use of action chains:
1.Get the action chain object first(也就是标签)
2.Set an action event on an action chain object(先设置的先执行)
3.The action is set to useperform执行
# Action chain module
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) # Used on action chain objectsclick_and_holdto hold backsourse标签元素
actions.drag_and_drop_by_offset(target,10,20)
# drag_and_drop_by_offset为将actionsobject to the distancetargetthe left side of the object10px、高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'] # between the two controlsx轴的距离
track=0
while track < distance:
ActionChains(driver).move_by_offset(xoffset=20,yoffset=0).perform()# 移动量为xoffset
track+=20 # This is just a control loop condition
ActionChains(driver).release().perform()
time.sleep(10)
finally:
driver.close()
二、xpath
Generally, the parsing library will have sub-methods for searching tags,一般都会支持css和xpath
XPath 是一门在 XML 文档中查找信息的语言
| 符号 | 作用 |
|---|---|
| 标签名 | Find the tag corresponding to the tag name,如div、p、a等 |
| / | Find the label under the current node |
| // | Find the label under the descendants of the current node |
| . | 表示当前节点 |
| … | 表示上一层 |
| @ | 表示取属性,如@id=‘xxx’、@href=‘www.baidu.com’ |
例如:
[@id="cnblogs_post_body"]/p[9]/strong
1.获取id为cnblogs_post_body的对象 [@id="cnblogs_post_body"]
2.Get the ninth in the current pathp标签 /p[9]
3.当前节点下的strong标签 /strong
三、打码平台使用
验证码的破解
简单的数字字母组合可以使用图像识别(python 现成模块),成功率不高
Use a third-party coding platform(破解验证码平台),花钱,把验证码图片给它,它给你识别完,返回给你
例如:
超级鹰
边栏推荐
- Button control switch 4017 digital circuit chip
- 《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
- 错误 AttributeError type object 'Callable' has no attribute '_abc_registry' 解决方案
- 【LeetCode】38、外观数列
- 1375. 二进制字符串前缀一致的次数-前序遍历法
- VBS函数应用–getobject的使用获得Automation对象
- Various problems with npm install
- 南瓜科学产品升级 开启益智探索新篇章
- Programmer Qixi Gift - How to quickly build an exclusive chat room for your girlfriend in 30 minutes
- 座舱人机交互「暗潮汹涌」,语音「下」,多模态「上」
猜你喜欢

Niuke.com Brush Question Record || Linked List

Execution failed for task ‘:xxx:generateReleaseRFile‘.

metaRTC5.0新版本支持mbedtls(PolarSSL)

AVR学习笔记之熔丝位

MPLS experiment

代码越写越乱?那是因为你没用责任链!

Redis 复习计划 - Redis主从数据一致性和哨兵机制

浙江大学团队使用基于知识图谱的新方法,从空间分辨转录组数据中推断细胞间通信状况

Week 7 Latent Variable Models and Expectation Maximization

理论篇1:深度学习之----LetNet模型详解
随机推荐
Win11快速助手在哪里?Win11打开快速助手的方法
Analysis and application of portrait segmentation technology
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
理论篇1:深度学习之----LetNet模型详解
Is the code more messy?That's because you don't use Chain of Responsibility!
Map常见的遍历方式-keySet 和 entrySet
南瓜科学产品升级 开启益智探索新篇章
Is there a replacement for the LM2596?LM2576 can
JSX使用
AutoCAD DWG,DXF文件导出高清图片、PDF
leetcode 48. Rotate Image (Medium)
错误 AttributeError type object 'Callable' has no attribute '_abc_registry' 解决方案
卷积神经网络 基础
节省50%成本!京东云重磅发布新一代混合CDN产品
c#之winform(软件开发)
vcl啥意思_oval
【牛客刷题-SQL大厂面试真题】NO5.某宝店铺分析(电商模式)
文字编码 - Markdown 简明教程
记录都有哪些_js常用方法总结
解题-->在线OJ(十八)