模拟登陆12306

模拟登陆12306,第1张

概述1.封装在线打码平台--超级鹰 import requestsfrom hashlib import md5class Chaojiying_Client(object): def __init__(self, username, password, soft_id): self.username = username password

1.封装在线打码平台--超级鹰


import
requestsfrom hashlib import md5class 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,headers=self.headers) return r.Json()

2.

def get_img_text(imgPath):    chaojiying = Chaojiying_ClIEnt(bobo328410948,bobo328410948,899370)#用户中心>>软件ID 生成一个替换 96001    im = open(imgPath,rb).read()#本地图片文件路径 来替换 a.jpg 有时win系统须要//    return chaojiying.PostPic(im,9004)[pic_str]

3.对12306进行模拟登录:验证码图片必须通过裁剪图片的形式获取

3.1.浏览器实例化 bro = webdriver.Chrome(executable_path=./chromedriver.exe) 

3.2.发起请求 bro.get(https://kyfw.12306.cn/otn/login/init) 

 

3.3.输入用户名和密码

3.4.标签定位验证图片 code_img_ele=bro.find_element_by_xpath(//*@ID="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img) 

3.5.验证码  左上角坐标location location = code_img_ele.location 

3.6.验证码  长度size size = code_img_ele.size 

3.7.指定矩形区域

rangle=(int(location[x]),int(location[y]),int(location[x]+size[wIDth]),int(location[y]+size[height]))

 

3.8.截图整张页面save_screenshot()

3.9.指定区域截图crop

3.10.调用超级鹰对验证码进行识别  

3.11.将x,y变成[[x,y]]

3.12.基于动作链,将x,y的参照系转移到指定的标签中  move_to_element_with_offse

3.13.点击登录

from selenium import webdriverfrom lxml import etreefrom selenium.webdriver import ActionChainsfrom PIL import Imagefrom time import sleepbro = webdriver.Chrome(executable_path=./chromedriver.exe)bro.get(https://kyfw.12306.cn/otn/login/init)sleep(2)bro.find_element_by_ID(username).send_keys(用户名)bro.find_element_by_ID(password).send_keys(密码)#想要获取验证码图片左上角和右下角亮点坐标,通过这亮点坐标可以形成一个裁剪的矩形区域code_img_ele = bro.find_element_by_xpath(//*[@ID="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img)location = code_img_ele.location  # 验证码图片左上角坐标size = code_img_ele.size #验证码图片的长宽#指定矩形区域rangle = (int(location[x]),int(location[y]+size[height]))
#截图bro.save_screenshot(aa.png)
#在指定区域截图cropi = Image.open(./aa.png)code_img_name = code.pngframe = i.crop(rangle)frame.save(code_img_name)
#进行验证码的识别,调用超级鹰result = get_img_text(code_img_name)print(result)# x1,y1|x2,y2|x3,y3 x,y#[[x1,y1],[x2,y2],[x3,y3]] [[x,y]]
#将x,y]]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)
for l in all_List: #x,y就是需要点击的某一个点的坐标 x = l[0] y = l[1] #move_to_element_with_offset就是将x,y的参照系转移到指定的标签中 #每一个动作连的 *** 作都必须基于一个单独的动作连 ActionChains(bro).move_to_element_with_offset(code_img_ele,x,y).click().perform()sleep(2)bro.find_element_by_ID(loginSub).click()sleep(10)bro.quit()
总结

以上是内存溢出为你收集整理的模拟登陆12306全部内容,希望文章能够帮你解决模拟登陆12306所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1066602.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存