点触验证

# 账户设置

class Settings(object):

    def __init__(self):
        self.chaojiying_user = ""
        self.pw = ''
        self.dxuser = ""
        self.chaojiying_soft_id = ""
        self.geetest_user = ''
# 超级鹰接口
# https://www.chaojiying.com/api-14.html

import requests
from hashlib import md5
from settings import Settings

settings = Settings()

class Chaojiying_Client(object):

    def __init__(self,username,password,soft_id):
        self.username = username
        self.password = md5(password.encode('utf-8')).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()


# if __name__ == '__main__':
# 	chaojiying = Chaojiying_Client(settings.user, settings.pw,'901971')	#用户中心>>软件ID 生成一个替换 96001
# 	im = open('a.jpg', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
# 	print(chaojiying.PostPic(im, 1902))											#1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
#点触
#http://www.geetest.com/
from selenium import webdriver
from selenium.webdriver import ActionChains
from settings import Settings
import time
from PIL import Image
from io import BytesIO
from chaojiyingK import Chaojiying_Client

settings = Settings()
geetest_user = settings.geetest_user
geetest_pw = settings.pw
chaojiying_user = settings.chaojiying_user
chaojiying_pw = settings.pw
chaojiying_soft_id = settings.chaojiying_soft_id
CHAOJIYING_KIND = 9004 #验证类型

class CrackTouClick():
    def __init__(self):
        """
        初始化
        """
        self.url = 'https://account.geetest.com/login/'
        self.browser = webdriver.Chrome()
        self.geetest_user = geetest_user
        self.geetest_pw = geetest_pw
        self.chaojiying = Chaojiying_Client(chaojiying_user,chaojiying_pw,chaojiying_soft_id)

    def  __del__(self):
        time.sleep(10)
        self.browser.close()

    def open(self):
        """
        打开网页输入用户名及密码
        :return: None
        """
        self.browser.get(self.url)
        input_user = self.browser.find_elements_by_class_name('ivu-input')[0].send_keys(self.geetest_user)
        input_pw = self.browser.find_elements_by_class_name('ivu-input')[1].send_keys(self.geetest_pw)

    def get_touclick_button(self):
        """
        获取初始化验证按钮
        :return:
        """
        button = self.browser.find_element_by_class_name('geetest_radar_tip')
        return button

    def get_touclick_element(self):
        """
        获取验证图片对象
        :return: 图片对象
        """
        element = self.browser.find_element_by_class_name('geetest_fullpage_click_box')
        return element

    def get_position(self):
        """
        获取验证码位置
        :return: 验证码位置元组
        """
        element = self.get_touclick_element()
        time.sleep(2)
        location = element.location
        size = element.size
        top, bottom, left, right = location['y'], location['y'] + size['height'], \
                                   location['x'], location['x'] + size['width']
        return top, bottom, left, right

    def get_screenshot(self):
        """
        获取网页截图
        :return: 截取对象
        """
        screenshot = self.browser.get_screenshot_as_png()
        screenshot = Image.open(BytesIO(screenshot))
        return screenshot

    def get_touclick_image(self,name = 'captcha.png'):
        """
        获取验证码图片
        :param name: 文件名字
        :return: 图片对象
        """
        top, bottom, left, right = self.get_position()
        print("验证码位置:",top, bottom, left, right)
        screenshot = self.get_screenshot()
        captcha = screenshot.crop((left,top,right,bottom))
        captcha.save(name)
        return captcha

    def get_points(self,captcha_result):
        """
        解析识别结果
        :param captcha_result: 识别结果
        :return: 转化后的结果
        """
        groups = captcha_result.get('pic_str').split('|')
        locations = [[int(number) for number in group.split(',')]for group in groups]
        return locations

    def touch_click_word(self,locations):
        """
        点击验证图片
        :param locations:点击位置
        :return: None
        """
        for location in locations:
            print(location)
            ActionChains(self.browser).move_to_element_with_offset(self.get_touclick_element(),location[0],
                                                                   location[1]).click().perform()
            time.sleep(1)
        time.sleep(5)
        button = self.browser.find_element_by_class_name('geetest_commit')
        button.click()

    def touch_click_verify(self):
        """
        点击验证按钮
        :return: None
        """
        button = self.browser.find_element_by_class_name('geetest-btn')
        button.click()
        time.sleep(2)
        print("成功登录")


    def crack(self):
        """
        主函数。
        :return:None
        """
        self.open()
        # 点击验证码图片
        button = self.get_touclick_button()
        button.click()
        # 获取验证码图片
        image = self.get_touclick_image()
        bytes_array = BytesIO()
        # 图片保存为字节流
        image.save(bytes_array,format='PNG')
        # print(image)
        # 识别验证码
        result = self.chaojiying.PostPic(bytes_array.getvalue(),CHAOJIYING_KIND)
        print(result)
        # 解析验证码
        locations = self.get_points(result)
        # 点击验证码
        self.touch_click_word(locations)
        # 点击登录
        self.touch_click_verify()


if __name__ == "__main__":
    crack = CrackTouClick()
    crack.crack()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值