class Settings(object):
def __init__(self):
self.chaojiying_user = ""
self.pw = ''
self.dxuser = ""
self.chaojiying_soft_id = ""
self.geetest_user = ''
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()
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')
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()