#-*- coding:utf-8 -*-#
#-------------------------------------------------------------------------
#ProjectName: Python2020
#FileName: Get_Code_Image.py
#Author: mutou
#Date: 2020/6/16 20:53
#Description:获取12306验证码图片
#--------------------------------------------------------------------------
from Day18.BaseModule.Base_Class import BaseClass
from selenium.webdriver.support.ui import WebDriverWait
import sys
import base64
import os
import time
from Day22.get_path import get_image_path
from Day22.Code12306.Click_Code import CickCode
from selenium.webdriver.common.action_chains import ActionChains
class GetCodeImage(BaseClass):
def __init__(self,url,browserType,image_name):
super().__init__(url,browserType)
self.image_name=image_name
self.image_path=os.path.join(get_image_path,image_name)
self.location=[[-110,-30],[-40,-30],[40,-30],[110,-30],[-110,50],[-40,50],[40,50],[110,50]]
#获取12306验证码图片;最简单的方式就是通过Pillow模块完成验证码图片从首页中进行截取出来,直接截取出来的图片会影响图片的质量
#将图片变得模糊;如何提高图片质量的问题:图片都是以二进制数据进行传输的,如果能够直接获取图片的原二进制数据的话, 然后将二进制数据转写成
#一张图片,那么中间就不会发生质量问题;
def get_code_image(self):
self.get_driver.find_element_by_class_name("login-hd-account").click()
try:
self.get_code_elment=WebDriverWait(self.get_driver,5).until(lambda driver:driver.find_element_by_id("J-loginImg"))
#此处src数据加载问题
time.sleep(3)
get_src=self.get_code_elment.get_attribute("src")
#得到base64格式编码数据
get_base64=get_src.split(",")[-1]
#获取到bytes对象,文件、图像实际都是基于byte流
get_byte=base64.b64decode(get_base64)
print(self.image_path)
with open(self.image_path,mode="wb") as fp:
#既可以实现以二进制流的形式读和写两种操作
fp.write(get_byte)
except:
print(sys.exc_info())
#定义一个方法完成验证码的点击操作
def click_code(self,server_path,type):
#获取到第三方返回的结果
get_result_list=CickCode(server_path,type).up_image(self.image_path)
print(get_result_list)
#提取鼠标创建的对象
action=ActionChains(self.get_driver)
#遍历对应的图
for i in get_result_list:
#因为可以根据鼠标移动到指定的坐标点完成点击操作
#创建鼠标对象;此处的i表示的是第几幅图,而下面传入location中表示的是索引
get_value=self.location[int(i)-1]
#设定的坐标点是相对真个验证码图片而言,所以可以先讲鼠标对象移动到当前验证码元素对象上,然后实现偏移点击坐标点
action.move_to_element(self.get_code_elment).move_by_offset(get_value[0],get_value[1]).click()
#上面此种实现相当于完成每一次点击都重新创建一个鼠标对象。在程序开发中,实际是尽量的减少循环中创建对象;
#如果将鼠标创建对象提取的话,则必须将执行的动作于整个循环完毕后执行,否则在循环中有可能整个动作就已经被执行,则后续动作无法执行;
action.perform()
if __name__ == '__main__':
get=GetCodeImage("https://kyfw.12306.cn/otn/resources/login.html","Chrome","code.jpg")
# print(get.image_path)
get.get_code_image()
get.click_code("http://littlebigluo.qicp.net:47720/","Chrome")
#注意:两个模块的相互引用容易造成模块初始化错误
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Code12306.zip (11个子文件)
Code12306
__init__.py 322B
get_path.py 507B
Code12306
__init__.py 322B
Get_Code_Image.py 4KB
image
code.jpg 14KB
Click_Code.py 2KB
__pycache__
Get_Code_Image.cpython-38.pyc 2KB
Click_Code.cpython-38.pyc 1KB
__init__.cpython-38.pyc 163B
__pycache__
get_path.cpython-38.pyc 277B
__init__.cpython-38.pyc 153B
共 11 条
- 1
资源评论
石明亮(HRJT)
- 粉丝: 255
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功