selenium 12306模拟登陆

 

代码应用场景 :基于第三方打码网站模拟登陆12306

  验证码识别

    基于第三方平台超级鹰识别

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

  超级鹰使用流程:

    注册 登陆(用户中心)充值

    创建一个软件:软件ID->生成一个软件ID(901977)

    下载实例代码->开发文档->python

1. 重新封装在打码平台下载到的python代码

#!/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()

2.登陆12306代码逻辑

from selenium import webdriver
from selenium.webdriver import ActionChains
from CJY import Chaojiying_Client
from time import sleep
from PIL import Image

#pip install Pillow

def get_code_text(imgPath,imgType):
    chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '创建的软件ID')  # 用户中心>>软件ID 生成一个替换 96001
    im = open(imgPath, 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
    return chaojiying.PostPic(im, imgType)

bro = webdriver.Chrome(executable_path='./chromedriver.exe')
bro.get('https://kyfw.12306.cn/otn/login/init')
sleep(2)
userName = bro.find_element_by_id('username')
userName.send_keys('12306账号')
sleep(2)
passWord = bro.find_element_by_id('password')
passWord.send_keys('12306密码')

# bro.set_window_size(800,600)
bro.set_window_size(1920,1080)

#验证码识别,实现点击操作
bro.save_screenshot('./main.png') #当前页面所对应的一整张图片

#验证码图片的标签
code_img_tag = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')

#验证码图片左下角坐标
location = code_img_tag.location #location返回的是位置
size = code_img_tag.size
# 裁剪区域
rangle = (int(location['x']),int(location['y']),int(location['x'])+size['width'],int(location['y']+size['height']))

i = Image.open('./main.png')
frame = i.crop(rangle)
frame.save('./code.png')

img_text = get_code_text('./code.png',9004)
img_text = img_text['pic_str']
print(img_text,type(img_text))

all_list = []
if '|' in img_text:
    list_1 = img_text.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)
        print(xy_list)
        all_list.append(xy_list)

else:
    x = int(img_text.split(',')[0])
    y = int(img_text.split(',')[1])
    xy_list = []
    xy_list.append(x)
    xy_list.append(y)
    all_list.append(xy_list)

print(all_list)
for p in all_list:
    x = p[0]
    y = p[1]
    # x ,y 就是即将要点击的坐标
    ActionChains(bro).move_to_element_with_offset(code_img_tag,x,y).click().perform()
    sleep(1)

login_btn = bro.find_element_by_xpath('//*[@id="loginSub"]')
login_btn.click()

sleep(5)
bro.quit()

ps: 如果不成功,最好是把自己电脑文字分辨率调整成100%

 

posted @ 2019-10-24 14:42  ivy_wang  阅读(284)  评论(0编辑  收藏  举报