12306模拟登陆-超级鹰

12306模拟登陆-超级鹰

什么是超级鹰?

就是和云打码类似的验证码识别网站,但是他能识别更复杂的图片验证码

模拟登陆12306

主要思路

1.首先登陆到12306界面

2.点击账号密码登录,到账号密码登录模块

3.截取整张界面的图片保存到本地,并获取验证码部分的坐标

4.在界面截图的基础上,根据获取的坐标截取验证码的图片保存到本地

5.把图片交给超级鹰,返回结果

6.把结果解析成想要点击的坐标

7.输入账号密码,执行验证码点击,点击登录

8.ok,但是有几率无法识别验证码

from selenium import webdriver
import chaojiying as cj
import time
#动作链,点击验证码
from selenium.webdriver import ActionChains
#图片处理,截图用
from PIL import Image
#实现规避检测
from selenium.webdriver import ChromeOptions
#12306登录url
url="https://kyfw.12306.cn/otn/resources/login.html"

#规避检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])

chrome=webdriver.Chrome(executable_path="chromedriver",options=option)
chrome.maximize_window()

#发起请求
chrome.get(url)
time.sleep(3)

#切换到账号密码登录
#找到切换按钮
button=chrome.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a')
#点击切换到账号密码登录
button.click()
time.sleep(5)

#获取验证码图片的标签对象
code_img=chrome.find_element_by_xpath('//*[@id="J-loginImg"]')
#获取验证码图片的左上角x,y坐标位置
img_location=code_img.location
#获取验证码图片的长宽
img_size=code_img.size
#图片左上角和右下角的坐标
rangle=(
    int(img_location['x']), int(img_location['y']),int(img_location['x'] + img_size['width']),int(img_location['y'] + img_size['height'])
)
time.sleep(2)

#截取整张界面的图片
chrome.save_screenshot("12306.png")
html_img=Image.open("12306.png")
time.sleep(2)

#设置截图后的名字
code_img_name="code.png"
#截取验证码部分的图片
frame=html_img.crop(rangle)
#保存验证码截图
frame.save(code_img_name)
time.sleep(2)

#将图片发给超级鹰识别获取结果,zx为封装的接口
result=cj.zx()

#解决返回结果,创建点击列表
all_location=[]
if '|' in result['pic_str']:
    lo_list=result['pic_str'].split('|')
    for i in lo_list:
        lo_list=[]
        x=int(i.split(',')[0])
        y=int(i.split(',')[1])
        lo_list.append(x)
        lo_list.append(y)
        all_location.append(lo_list)
else:
    lo_list = []
    x=int(result['pic_str'][0])
    y=int(result['pic_str'][1])
    lo_list.append(x)
    lo_list.append(y)
    all_location.append(lo_list)


for i in all_location:
    x=i[0]
    y=i[1]
    ActionChains(chrome).move_to_element_with_offset(code_img,x,y).click().perform()
    time.sleep(1)

#输入账号
chrome.find_element_by_id('J-userName').send_keys("******")
#输入密码
chrome.find_element_by_id('J-password').send_keys('******')
time.sleep(2)

#点击登录
chrome.find_element_by_id('J-login').click()
time.sleep(10)

chrome.quit()
原文地址:https://www.cnblogs.com/zx125/p/11488639.html