本节所讲内容:
Selenium+Python环境搭建及配置
滑块验证码步骤分解
QQ空间破解滑块验证码登录(全部代码请看最后)
1.1 selenium 介绍
1.2 selenium+Python环境配置
前提条件:已安装好Python开发环境(python3.7.2),这是最基本的呦~
安装步骤:
1. 安装selenium:pip install selenium
2. 安装webdriver
各大浏览器webdriver地址可参见:https://docs.seleniumhq.org/download/
Firefox:https://github.com/mozilla/geckodriver/releases/
Chrome:https://sites.google.com/a/chromium.org/chromedriver/ 或者http://chromedriver.storage.googleapis.com/index.html
IE:http://selenium-release.storage.googleapis.com/index.html
注:webdriver需要和对应的浏览器版本以及selenium版本对应
Webdriver版本
支持的Chrome版本
v2.41
v67-69
v2.40
v66-68
v2.39
v66-68
v2.38
v65-67
v2.37
v64-66
v2.36
v63-65
v2.35
v62-64
v2.34
v61-63
3. webdriver安装路径
Win:复制webdriver到Python安装目录下或者在path中配置下路径(方便python能快速寻找)
2. 滑块验证码步骤分解
第一步:访问QQ空间登录网址,定位iframe登录标签,点击账号密码
代码实现:
driver = webdriver.Chrome()
driver.set_window_position(900, 10)
driver.maximize_window()
driver.get(url)
#切换frame
driver.switch_to.frame('login_frame')
#点击
driver.find_element_by_id("switcher_plogin").click()
第二步:跳转到输入界面,然后清空每个输入框的数据然后在点击登录
代码实现:
# 输入用户名和密码
driver.find_element_by_id('u').clear()
driver.find_element_by_id('u').send_keys('3403073998')
driver.find_element_by_id('p').clear()
driver.find_element_by_id('p').send_keys('family521@#!')
sleep(1)
# 点击登录
driver.find_element_by_id('login_button').click()
sleep(5)
第三步:再次定位到iframe按钮操作并进行拖拽,距离我们保持固定180,图片让它更换底板图片
在这个地方需要注意点的是我们并没有对滑块移动的距离进行确定,
只是写一个distances=180这个固定的距离,
当然在这个拉取的过程中每次拉取也有可能失败,这是正常现象,请各位拿到代码后运行下
代码实现:
# 切换iframe
try:
iframe = driver.find_element_by_xpath('//iframe')
except Exception as e:
pass
sleep(2) # 等待资源加载
driver.switch_to.frame(iframe)
# 等待图片加载出来
WebDriverWait(driver, 5, 0.5).until(
EC.presence_of_element_located((By.ID, "tcaptcha_drag_button"))
)
try:
button = driver.find_element_by_id('tcaptcha_drag_button')
except Exception as e:
pass
sleep(1)
# 开始拖动 perform()用来执行ActionChains中存储的行为
#distance 代表的是滑块移动的距离,我们这里直接写死
distance = 180
times = 0
while True:
try:
action = ActionChains(driver)
#点击鼠标并进行拖拽
action.click_and_hold(button).perform()
# 清除之前的action
action.reset_actions()
#模拟轨迹方程
track = get_track(distance)
#开始模拟拖拽
for i in track:
#y轴不偏移,x轴持续滑动
action.move_by_offset(xoffset=i, yoffset=0).perform()
action.reset_actions()
sleep(0.5)
#释放鼠标
action.release().perform()
sleep(5)
times += 1
print('这是第{}次'.format(times))
except:
print('登录成功')
break
轨迹方程:(为了模仿人为的动作,防止整理)
def get_track(distance):
"""
根据偏移量获取移动轨迹
:param distance: 偏移量
:return: 移动轨迹
"""
# 移动轨迹
track = []
# 当前位移
current = 0
# 减速阈值
mid = distance * 4 / 5
# 计算间隔
t = 1
# 初速度
# v = 0
v = 0
while current < distance:
if current < mid:
# 加速度为正2
# a_b = 8
a = 10
else:
# 加速度为负3
a = -3
# 初速度v0
v0 = v
# 当前速度v = v0 + at
v = v0 + a * t
# 移动距离x = v0t + 1/2 * a_b * t^2
move = v0 * t + 1 / 2 * a * t * t
# 当前位移
current += move
# 加入轨迹
track.append(round(move))
return track
3. QQ空间破解滑块模拟登录全部代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2020/2/21 21:18
# @Author : Xuegod Teacher For
# @File : qq空间.py
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
url = 'https://i.qq.com'
def get_track(distance):
"""
根据偏移量获取移动轨迹
:param distance: 偏移量
:return: 移动轨迹
"""
# 移动轨迹
track = []
# 当前位移
current = 0
# 减速阈值
mid = distance * 4 / 5
# 计算间隔
t = 1
# 初速度
# v = 0
v = 0
while current < distance:
if current < mid:
# 加速度为正2
# a_b = 8
a = 10
else:
# 加速度为负3
a = -3
# 初速度v0
v0 = v
# 当前速度v = v0 + at
v = v0 + a * t
# 移动距离x = v0t + 1/2 * a_b * t^2
move = v0 * t + 1 / 2 * a * t * t
# 当前位移
current += move
# 加入轨迹
track.append(round(move))
return track
def main():
driver = webdriver.Chrome()
driver.set_window_position(900, 10)
driver.maximize_window()
driver.get(url)
driver.switch_to.frame('login_frame')
driver.find_element_by_id("switcher_plogin").click()
sleep(1)
# 输入用户名和密码
driver.find_element_by_id('u').clear()
driver.find_element_by_id('u').send_keys('你的QQ账号')
driver.find_element_by_id('p').clear()
driver.find_element_by_id('p').send_keys('你的QQ密码')
sleep(1)
# 点击登录
driver.find_element_by_id('login_button').click()
sleep(5)
# 切换iframe
try:
iframe = driver.find_element_by_xpath('//iframe')
except Exception as e:
pass
sleep(2) # 等待资源加载
driver.switch_to.frame(iframe)
# 等待图片加载出来
WebDriverWait(driver, 5, 0.5).until(
EC.presence_of_element_located((By.ID, "tcaptcha_drag_button"))
)
try:
button = driver.find_element_by_id('tcaptcha_drag_button')
except Exception as e:
pass
sleep(1)
# 开始拖动 perform()用来执行ActionChains中存储的行为
distance = 180
times = 0
while True:
try:
action = ActionChains(driver)
#点击鼠标并进行拖拽
action.click_and_hold(button).perform()
# 清除之前的action
action.reset_actions()
#模拟轨迹方程
track = get_track(distance)
#开始模拟拖拽
for i in track:
#y轴不偏移,x轴持续滑动
action.move_by_offset(xoffset=i, yoffset=0).perform()
action.reset_actions()
sleep(0.5)
#释放鼠标
action.release().perform()
sleep(5)
times += 1
print('这是第{}次'.format(times))
except:
print('登录成功')
break
print(driver.title)
sleep(2)
driver.quit()
print("finish~~")
if __name__ == '__main__':
main()