您的位置:首页 > 编程语言 > Python开发

Python Selenium自动登录12306官网

2020-08-09 23:13 1821 查看

超级鹰

超级鹰官网 http://www.chaojiying.com/

超级鹰功能

1.超级鹰图片分类及识别录入系统
2.独立的数据及统计服务,实时与总部数据中心直连。
3.整合云录入客户端,提供更高的安全性及工作效率。
4.采用世界先进的图片处理算法以及神经网络训练系统来提供识别率

超级鹰使用步骤

1.注册:普通用户

2.登录:普通用户
3.题分查询:充值(做实验充值1块钱就够)

4.创建一个软件(id)

5.下载示例代码

12306官网模拟登录:

编码流程

1.使用selenium打开登录页面
2.对当前selenium打开的这张页面进行截图
3.对当前图片局部区域(验证码图片)进行裁剪(作用:将验证码图片和模拟登录进行一一对应。)

  1. 使用超级鹰识别验证码图片(坐标)
    5.使用动作链根据坐标实现点击操作
    6 录入用户名密码,点击登录按钮实现登录

编写代码

#超级鹰代码(直接拷贝即可)
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()

#使用selenium打开登录页面

from selenium import webdriver
import time
from PIL import Image    #裁剪图片模块
from selenium.webdriver import ActionChains   #动作链

bro = webdriver.Chrome(executable_path='./chromedriver')
# 浏览器最大化,也可以不设置
bro.maximize_window()
#现在使用这个url地址
bro.get('https://kyfw.12306.cn/otn/resources/login.html')
time.sleep(1)
bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()  #使用账号登录
time.sleep(1)

#save_screenshot就是将当前页面进行截图且保存
bro.save_screenshot('aa.png')

#确定验证码图片对应的左上角和右下角的坐标(裁剪的区域就确定)
code_img_ele = bro.find_element_by_xpath('//*[@id="J-loginImg"]')

location = code_img_ele.location  # 验证码图片左上角的坐标 x,y
print('location:',location)
size = code_img_ele.size  #验证码标签对应的长和宽
print('size:',size)

#左上角和右下角坐标
rangle = (
int(location['x']), int(location['y']), int(location['x']+size['width'] ), int(location['y']+size['height'] ))

#至此验证码图片区域就确定下来了
i = Image.open('./aa.png')
code_img_name = './code.png'
#crop根据指定区域进行图片裁剪
frame = i.crop(rangle)
frame.save(code_img_name)

#将验证码图片提交给超级鹰进行识别
chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰密码', '软件ID')   #用户中心>>软件ID 生成一个替换 96001
im = open('code.png', 'rb').read()  #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 9004)['pic_str'])
result = chaojiying.PostPic(im, 9004)['pic_str']
all_list = [] #要存储即将被点击的点的坐标  [[x1,y1],[x2,y2]]

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)
print(all_list)
#遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置进行点击操作
for l in all_list:
x = l[0]
y = l[1]
ActionChains(bro).move_to_element_with_offset(code_img_ele, x, y).click().perform()
time.sleep(1)

bro.find_element_by_id('J-userName').send_keys('用户名')
time.sleep(1)
bro.find_element_by_id('J-password').send_keys('密码')
bro.find_element_by_id('J-login').click()
time.sleep(1)
div=bro.find_element_by_id('nc_1_n1z')

#动作链
action = ActionChains(bro)
#点击长按指定的标签
action.click_and_hold(div)

#处理滑动模块
for i in range(5):
#perform()立即执行动作链操作
#move_by_offset(x,y):x水平方向 y竖直方向
action.move_by_offset(30,0).perform() #速度为30mm
sleep(0.5)

#释放动作链
action.release()

运行效果:

成功登录

注意事项:

1.使用超级鹰前先看账户余额
2.运行程序前要先将自己电脑显示设置为100%,否则识别验证码一定会出错!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: