爬虫_07_12306模拟登录

07_12306模拟登录

网站地址:https://kyfw.12306.cn/otn/resources/login.html/init

  1. 使用selenium打开登录页面
  2. 对当前selenium打开的这张页面进行截图
  3. 对当前图片局部区域(验证码图片)进行裁剪
    • 好处:将验证码图片和模拟登录进行一一对应
  4. 使用超级鹰识别验证码图片(坐标)
#...
#上述代码为超级鹰提供的示例代码

#使用selenium打开登录页面
from selenium import webdriver
import time

bro = webdriver.Chrome(executable_path='./chromedriver')
bro.get('https://kyfw.12306.cn/otn/login/init')
time.sleep(1)

#save_screenshot就是将当前页面进行截图且保存
bro.save_screenshot('aa.png')

#确定验证码图片对应的左上角和右下角的坐标(裁剪的区域就确定)
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 # 验证码图片左上角的坐标x,y
size = code_img_ele.size # 验证码标签对应的长和宽
#左上角和右下角坐标
rangle = (
    int(location['x']),int(location['y']),int(location['x'] + size['width']),int(location['y'] + size['height'])
)
#至此验证码图片区域就确定下来了

from PIL import Image
#
i = Image.open('aa.png')
code_img_name = './code.png'
#crop根据指定区域进行图片裁剪
frame = i.crop(rangle)
frame.save(code_img_name)

#将验证码图片提交给超级鹰进行识别
chaojiying = Chaojiying_Client('账号','密码','secret')
im = open('code.jpg','rb').read()
result = chaojiying.PostPic(im,9004)['pic_str']
print(result)
#锣 253,83|253,153

from selenium.webdriver import ActionChains
all_list = [] #要存储即将被点击的点的坐标 [[x1,y1],[x2,y2]]
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)
print(all_list)
#遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置进行点击操作
for l in all_list:
    x = l[0]
    y = l[1]
    #相对于参照物(验证码栏)进行点击
    ActionChains(bro).move_to_element_with_offset(code_img_ele,x,y).click().perform()
    time.sleep(0.5)

#录入用户名和密码
bro.find_element_by_id('username').send_keys('xxxxxxx')
time.sleep(2)
bro.find_element_by_id('password').send_keys('xxxxxxx')
time.sleep(2)
bro.find_element_by_id('loginSub').click()
time.sleep(10)
bro.quit()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值