python爬虫入门学习9-selenium+超级鹰12306模拟登录过验证码

from util import chaojiying_util as cu
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from PIL import Image

# 计算xy坐标方法
def calxy(result_list):
    x_list = []
    y_list = []
    if '|' in result_list:
        list_list = result_list.split('|')
        for i in range(len(list_list)):
            split_list(list_list[i], x_list, y_list)
    else:
        split_list(result_list, x_list, y_list)
    return x_list, y_list


def split_list(result_list, x_list, y_list):
    in_list = result_list.split(',')
    x_list.append(int(in_list[0]))
    y_list.append(int(in_list[1]))
    return x_list, y_list
    
url = 'https://kyfw.12306.cn/otn/resources/login.html'
# 实例化浏览器对象 浏览器驱动程序
bro = webdriver.Chrome(executable_path='chromedriver.exe')

# 发起请求
bro.get(url=url)
# 最大化窗口
bro.maximize_window()
time.sleep(2)
# 进入页面从扫码登录切换到帐号密码登录
a_tag = bro.find_element_by_class_name('login-hd-account')
a_tag.click()
time.sleep(2)

# 截图整个页面用来截取验证码图片 页面图片上是文件流加密了
bro.save_screenshot('windows.png')
# 根据验证码图片 获取左上角坐标和图片长宽
loginImg = bro.find_element_by_xpath('//*[@id="J-loginImgArea"]')
location = loginImg.location  # 验证码图片左上角坐标
size = loginImg.size  # 验证码的尺寸 长宽
# 根据数据 计算右下角的长宽坐标
rangle = (
    location['x'],
    location['y'],
    location['x'] + size['width'],
    location['y'] + size['height']
)
print('验证码图片坐标',rangle)

# 根据坐标截取验证码图片
img = Image.open('./windows.png')
code_img_name = 'code.png'
frame = img.crop(rangle)
frame.save(code_img_name)

# 这里是直接调用了超级鹰的方法解析验证码 
result_list = cu.post_pic('./code.png', 9004)['pic_str']
# 切割获取验证码图片答案坐标
x_list, y_list = cu.calxy(result_list)
print(all_list)

# 循环坐标移动并点击
for x, y in zip(x_list, y_list):
    print(x, y)
    ActionChains(bro).move_to_element_with_offset(loginImg, x, y).click().perform()

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

你可能感兴趣的:(爬虫,python,selenium,python)