验证码(抠图)和超级鹰的使用
-
获取验证码
from selenium.webdriver import Chrome, ChromeOptions import time from chaojiying import ChaojiyingClient # 使用PIL前需要安装:pillow from PIL import Image options = ChromeOptions() # 设置取消测试环境 options.add_experimental_option('excludeSwitches', ['enable-automation']) b = Chrome(options=options) def get_data(): url = 'https://passport.bi***i.com/' b.get(url) # print(b.page_source) time.sleep(1) # 用户名 username = b.find_element_by_css_selector('#login-username') # 密码 pw = b.find_element_by_css_selector('#login-passwd') # 输入用户名和密码 username.send_keys('1392302') pw.send_keys('123456') # 获取登录按钮 login_btn = b.find_element_by_css_selector('.btn.btn-login') # 点击登录按钮 login_btn.click() time.sleep(1) # 获取验证码盒子 code_div = b.find_element_by_css_selector('.geetest_panel_next') location = code_div.location size = code_div.size x, y = location['x'], location['y'] w, h = size['width'], size['height'] # 截屏 screen_data = b.get_screenshot_as_png() print(type(screen_data)) open('files/c.png', 'wb').write(screen_data) # 抠图 # a.获取原图 image_data = Image.open('files/c.png') # image_data.show() # b.剪切 code_data = image_data.crop((x*2, y*2, x*2+w*2, y*2+h*2)) # code_data.show() # 保存验证码图片 code_data.save('files/d.png') # 调用超级鹰 获取验证码信息 # cjy = ChaojiyingClient('用户账号', '用户密码', '后台参数') # result = cjy.post_pic(需要识别验证码的图片的地址, 9004) # print(result) # time.sleep(10) if __name__ == '__main__': get_data()
-
滑动滑块
from selenium.webdriver import Chrome, ChromeOptions, ActionChains import time from PIL import Image import io from chaojiying import ChaojiyingClient # bytes -> (只读)字节流 -> BytesIO(读写)字节流 cjy = ChaojiyingClient('yuting', '123456', '915035') def get_data(): options = ChromeOptions() # 设置取消测试环境 options.add_experimental_option('excludeSwitches', ['enable-automation']) b = Chrome(options=options) b.get('https://passport.******.cm') btn = b.find_element_by_css_selector('.login-tab.login-tab-r') btn.click() time.sleep(1) name = b.find_element_by_css_selector('#loginname') pw = b.find_element_by_css_selector('#nloginpwd') name.send_keys('账号') pw.send_keys('密码') login_btn = b.find_element_by_css_selector('#loginsubmit') login_btn.click() # 获取验证码的位置和大小 code_dive = b.find_element_by_css_selector('.JDJRV-bigimg') location = code_dive.location size = code_dive.size x, y = location['x'], location['y'] w, h = size['width'], size['height'] # 截屏 screen_data = b.get_screenshot_as_png() open('files/screen.png', 'wb').write(screen_data) # 抠图 image = Image.open('files/screen.png') code_image = image.crop((x*2, y*2, x*2 + w*2, y*2 + h*2)) # code_image.thumbnail((w, h)) code_image.save('files/code.png') time.sleep(1) # 获取验证坐标 pic_data = open('files/code.png', 'rb').read() result = cjy.post_pic(pic_data, 9101) print(result) code_x, code_y = result['pic_str'].split(',') code_x = int(code_x) # 滑动滑块 slider = b.find_element_by_css_selector('.JDJRV-slide-inner.JDJRV-slide-btn') action = ActionChains(b) count = code_x//2//10 # 一步到位的拖拽 # drag_and_drop_by_offset(拖拽目标标签, 在x方向的位移, 在y方向的位移) # action.drag_and_drop_by_offset(slider, int(code_x)//2, 0) # action.release(slider) # action.perform() # 执行动作 # 一点一点的拖拽 action.click_and_hold(slider) for x in range(count): action.move_by_offset(10, 0) time.sleep(0.5) action.release(slider) action.perform() time.sleep(10) get_data()
-
超级鹰(py文件)
import requests from hashlib import md5 class ChaojiyingClient(object): def __init__(self, username, password, soft_id): self.username = username password = password.encode('utf8') self.password = md5(password).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 post_pic(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 report_error(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__': # 1.创建超级鹰对象 chaojiying = ChaojiyingClient('用户账号', '密码', '915035') #用户中心>>软件ID 生成一个替换 96001 # 2.准备验证码图片数据 im = open('files/code.png', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要// # # 3.发送验证码图片给平台 # print(chaojiying.post_pic(im, 9101)) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加() chaojiying.report_error('1137615106023600021') # {'err_no': 0, 'err_str': 'OK', 'pic_id': '1137611336023600007', 'pic_str': '289,504|363,378', 'md5': '578486d0431710fbc8b8d4f3f544d9bb'}