#!/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))
from selenium import webdriver
import time
from PIL import Image
from selenium .webdriver import ActionChains
bro = webdriver.Chrome(executable_path = './chromedriver')
bro.get('htttps://kyfw.12306.cn/otn/login/init')
time.sleep(1)
#将当前页面进行载图并进行保存
bro.save_screenshot('aa.png')
#确定验证码图片对应的左上角右下角的坐标(裁剪的区域确定)
code_img_ele=bro.find_element_by_xpath('验证码图片标签')
location = code_img_ele.location #验证码左上角的坐标x,y
size = code_img_ele.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('超级鹰用户名', '超级鹰用户名的密码', '96001') #用户中心>>软件ID 生成一个替换 96001
im = open('code.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print (chaojiying.PostPic(im, 9004)['pic_str'])
result = chaojiying.PostPic(im, 9004)['pic_str']
all_list = [] #要存储即将被点击的点坐标
if '|' in result:
list_1 = result.splist('|')
count_1 = len(list_1)
for i in range(count_1):
xy_list = []
x = int(list_1[i].splist(',')[0])
y = int(list_1[i].splist(',')[1])
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
else:
x = int(result.splist(',')[0])
y = int(result.splist(',')[1])
xy_list = []
xy_list.append(x)
xy_list.append(y)
all_list.append(xy_list)
print(all_list)
#遍历列表使用动作链对每一个列表元素对应的x,y指定的位置进行点击操作
for i in all_list:
x = i[0]
y = i[1]
ActionChains(bro).move_to_element_with_offset(code_img_ele,x,y).click().perform()
time.sleep(0.5)
#录入12306用户名或者密码
selenium模拟用户登录12306 实例
最新推荐文章于 2022-07-19 20:41:49 发布