一、前期准备
- 需要借助的资源
1、需要借助第三方的验证码识别工具进行识别,此处使用超级鹰做演示(http://www.chaojiying.com/)
2、注册超级鹰,购买积分,生成软件id - 思路整理
1、使用selenium打开登录界面
2、使用selenium进行截图
3、使用超级鹰获取验证码的坐标信息
4、使用selenium进行自动登录
二、代码如下
from selenium import webdriver
from PIL import Image
from selenium.webdriver import ActionChains
from selenium.webdriver import ChromeOptions
import time
import os
from selenium_test.chaojiying import Chaojiying_Client
url = 'https://kyfw.12306.cn/otn/resources/login.html'
#实现规避检测是否使用了selenium
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
driver = webdriver.Chrome(options=option)
driver.get(url)
# 防止12306禁止selenium
script = 'Object.defineProperty(navigator,"webdriver",{get:()=>undefined,});'
driver.execute_script(script)
driver.maximize_window()
#点击登录切换到账号登录的形式
driver.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
time.sleep(3)
#将当前页面截图保存
if os.path.exists('12306.png'):
os.remove('12306.png')
driver.save_screenshot('12306.png')
code_image_ele = driver.find_element_by_xpath('//*[@id="J-loginImg"]')
#验证码图片左上角的坐标x,y
location_left = code_image_ele.location
#获取图片的长和宽
image_size = code_image_ele.size
#验证码图片左上角和右下角的坐标
rangle = (int(location_left['x']),int(location_left['y']),int(location_left['x']+image_size['width']),
int(location_left['y']+image_size['height']))
#截取验证码区域的图片
i = Image.open('12306.png')
code_img_name = 'code.png'
frame = i.crop(rangle)
if os.path.exists('code.png'):
os.remove('12306.png')
frame.save(code_img_name)
#将验证码图片提交给超级鹰进行识别
chaojiying = Chaojiying_Client('账号', '密码', '软件ID')
im = open('code.png', 'rb').read()
result = chaojiying.PostPic(im, 9004)['pic_str']
# print(result)
#根据超级鹰返回的坐标进行图片选择
#因为超级返回多个坐标的时候使用| 进行分割
all_list = []
if '|' in result:
list_1 = result.split('|')
count_1 = len(list_1)
for i in range(count_1):
xy_list = []
x = int(list_1[i].split(',')[0])
y = int(list_1[i].split(',')[1])
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
else:
x = int(result.split(',')[0])
y = int(result.split(',')[1])
xy_list = []
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
#遍历列表,使用动作链对每一个元素对应的xy指定的位置进行操作
for i in all_list:
x = i[0]
y = i[1]
ActionChains(driver).move_to_element_with_offset(code_image_ele,x,y).click().perform()
#输入用户名密码点击登录
driver.find_element_by_xpath('//*[@id="J-userName"]').send_keys('xxxxxx@qq.com')
driver.find_element_by_xpath('//*[@id="J-password"]').send_keys('xxxxxx')
driver.find_element_by_xpath('//*[@id="J-login"]').click()
time.sleep(2)
#获取滑块的位置
hk_ele = driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')
hk_lefe = hk_ele.location
hk_size = hk_ele.size
#滑块的宽度
hk_width = hk_size['width']
#获取滑道的位置信息
yzm_img_ele = driver.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span')
img_lefe = yzm_img_ele.location
img_size = yzm_img_ele.size
#需要滑动的距离
distance = img_size['width'] - hk_width
# 移动轨迹
track=[]
# 当前位移
current=0
# 减速阈值
mid=distance*4/5
# 计算间隔
t=0.5
# 初速度
v=2
while current<distance:
if current<mid:
# 加速度
a=6
else:
# 加速度
a=-3
v0=v
# 当前速度
v=v0+a*t
# 移动距离
move=v0*t+1/2*a*t*t
# 当前位移
current+=move
# 加入轨迹
track.append(round(move))
#滑动滑块
ActionChains(driver).click_and_hold(hk_ele).perform()
for x in track:
ActionChains(driver).move_by_offset(xoffset=x,yoffset=0).perform()
time.sleep(0.5)
ActionChains(driver).release().perform()
超级鹰的代码:
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class Chaojiying_Client(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
if __name__ == '__main__':
chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰用户名的密码', '96001') #用户中心>>软件ID 生成一个替换 96001
im = open('a.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 1902)) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()