import json
from urllib import request
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
import requests
import base64
from io import BytesIO
from PIL import Image as image
from sys import version_info
from selenium.webdriver import ActionChains
# 从selenium.webdriver.common.by 导入By包进行元素定位
from selenium.webdriver.common.by import By
# 登录界面的url
url = 'https://lpt.liepin.com/user/login/'
# 实例化一个chrome浏览器
browser = webdriver.Chrome("D:\download_\chromedriver_win32\chromedriver")
# 用户名
username = ''
# 密码
password = ''
# 设置等待超时
wait = WebDriverWait(browser, 20)
def login():
# 窗口最大化
browser.maximize_window()
# 打开登录页面
browser.get(url)
# 获取用户名输入框
# EC判断元素是否存在,presence_of_element_located加载
# 点击密码登录
browser.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[2]/div/div/ul/li[3]').click()
user = wait.until(
EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/div[1]/div[2]/div/div/div/div[1]/div[1]/input')))
# 获取密码输入框
passwd = wait.until(
EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/div[1]/div[2]/div/div/div/div[1]/div[2]/input')))
# 输入用户名
user.send_keys(username)
# 输入密码
passwd.send_keys(password)
browser.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[2]/div/div/div/div[2]/button').click()
def get_x(uname, pwd, img_bgk):
img_bgk = img_bgk.convert('RGB')
buffered_img_bgk = BytesIO()
img_bgk.save(buffered_img_bgk, format("JPEG"))
if version_info.major >= 3:
b64_img_bgk = str(base64.b64encode(buffered_img_bgk.getvalue()), encoding='utf-8')
else:
b64_img_bgk = str(base64.b64encode(buffered_img_bgk.getvalue()))
data = {"username": uname, "password": pwd, "image": b64_img_bgk, "typeid": 33}
result = json.loads(requests.post("http://api.ttshitu.com/base64", json=data).text)
if result['success']:
return result["data"]["result"]
else:
return result["message"]
return ""
# 取原背景图不需要裁剪了
# def get_img(img_bgk_path):
# # 裁剪bgk_img
# img_bgk1 = image.open(img_bgk_path)
# # 按缩放比例 算出要裁剪的距离左边的x值
# x_1 = int(27 / (340 / 426))
# x = x_1 + 85
# img_bgk = img_bgk1.crop((x, 0, 426, 248)) # (left, upper, right, lower)
# return img_bgk
def get_trace(distance):
'''
:param distance: (Int)缺口离滑块的距离
:return: (List)移动轨迹
'''
# 创建存放轨迹信息的列表
trace = []
# 设置加速的距离
faster_distance = distance * (4 / 5)
# 设置初始位置、初始速度、时间间隔
start, v0, t = 0, 1, 0.5
# 当尚未移动到终点时
while start < distance:
# 如果处于加速阶段
if start < faster_distance:
# 设置加速度为2
a = 2
# 如果处于减速阶段
else:
# 设置加速度为-8
a = -8
# 移动的距离公式
move = v0 * t + 1 / 2 * a * t * t
# 此刻速度
v = v0 + a * t
# 重置初速度
v0 = v
# 重置起点
start += move
# 将移动的距离加入轨迹列表
trace.append(round(move))
# 返回轨迹信息
return trace
def move_(trace):
# 得到滑块标签
slider = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="tcaptcha_drag_thumb"]')))
# 使用click_and_hold()方法悬停在滑块上,perform()方法用于执行
ActionChains(browser).click_and_hold(slider).perform()
for x in trace:
# 使用move_by_offset()方法拖动滑块,perform()方法用于执行
ActionChains(browser).move_by_offset(xoffset=x, yoffset=0).perform()
# 模拟人类对准时间
sleep(0.5)
# 释放滑块
ActionChains(browser).release().perform()
def main():
login()
sleep(2)
# iframe=browser.find_element_by_xpath('//*[@id="tcaptcha_iframe"]')
# 切换到滑块窗体iframe
browser.switch_to_frame('tcaptcha_iframe')
# 获取背景图片(滑块图片一样)
img_object = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="slideBg"]')))
# 获取该WebElement下指定元素,
img_url = img_object.get_attribute('src')
# 打开链接
req = request.urlopen(img_url)
# 打开图片
img_bgk = image.open(req)
# 获取图片的大小,680x390
x, y = img_bgk.size
# print(x,y)
# 这个接口返回的是缺口左上角距离背景图左边的距离,
result = get_x(uname='', pwd='', img_bgk=img_bgk)
# 在我电脑上实时背景图显示的是 :340.8X72
# 滑块起始位置为:27-27.5px
# x轴位移量(根据自己情况缩放调整)
move_x = int((int(result) - (27 / (340 / x))) * (340 / 680)) # 可以+/-误差值
# x = int((int(result) + 85) * (340 / 426))
trace = get_trace(move_x)
move_(trace)
if __name__ == "__main__":
main()
滑动的速度要控制好 不然滑两把就被检测了