python利用selenium库识别点触验证码

利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路)

一、超级鹰注册:超级鹰入口

  1、首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分,基本上就够学习使用了。如果想一直用可以用,可以充值,不是很贵。

  2、下载超级鹰的python库代码。代码

  3、然后有测试案例,自己可以试着跑一跑代码。

二、使用selenium库来识别点触式验证码:

  1、首先是找一个使用点触式二维码的网站:(这个真的是比较难找了,由于静谧大大书上的网站被封了,我找了好久,才找到斗鱼的找回密码是用的点触式验证码,将就着用吧)。

  2、开始操作:

    (1)首先声明一个类,定义属性:

      

 1 ‘‘‘
 2     func:斗鱼找回密码,点触式二维码
 3     author:monty
 4     date:2018/11/24
 5 ‘‘‘
 6 from chaojiying import Chaojiying_Client
 7 from selenium import webdriver
 8 from selenium.webdriver.support.wait import WebDriverWait
 9 from selenium.webdriver.support import expected_conditions as EC
10 from selenium.webdriver.common.by import By
11 import time
12 from PIL import Image
13 from io import BytesIO
14 from selenium.webdriver import ActionChains
15
16 #填写自己的斗鱼注册手机号
17 tel=
18 #超级鹰的类型码
19 kind=9004
20 class CrackGeetest():
21     def __init__(self):
22         self.url=‘https://www.douyu.com/member/findpassword/findByPhone‘
23         self.browser=webdriver.Chrome()
24         self.browser.get(self.url)
25         self.wait=WebDriverWait(self.browser,20)
26         self.tel=tel
27         self.chaojiying=Chaojiying_Client(‘超级鹰账号‘, ‘超级鹰密码‘,kind)

    (2)填写输入框信息:

1     def set_tel(self):
2         ‘‘‘
3         填写telephonenumber
4         :return:
5         ‘‘‘
6         #获取输入框
7         input=self.wait.until(EC.presence_of_element_located((By.ID,‘reg_userphone‘)))
8         input.clear()
9         input.send_keys(self.tel)

   (3)获得初始的机器验证按钮:

    

1     def get_geetest_button(self):
2         ‘‘‘
3             获取初始验证按钮
4         :return:
5         ‘‘‘
6         button=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,‘geetest_radar_tip‘)))
7         return button

    (4)获取点触验证码图片:

  

 1     def get_image(self):
 2         ‘‘‘
 3             获取验证码图片
 4         :return: 图片对象
 5         ‘‘‘
 6         image=self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,‘geetest_widget‘)))
 7         return image
 8
 9     def get_position(self):
10         #获取图片的位置信息
11         image=self.get_image()
12         time.sleep(2)
13         location=image.location
14         size=image.size
15         top,bottom,left,right=location[‘y‘],location[‘y‘]+size[‘height‘]-55,location[‘x‘],location[‘x‘]+size[‘width‘]
16         return (top,bottom,left,right)
17
18     def get_screenshot(self):
19         ‘‘‘
20         获取整个屏幕截屏
21         :return:
22         ‘‘‘
23         screenshot=self.browser.get_screenshot_as_png()
24         screenshot=Image.open(BytesIO(screenshot))
25         return screenshot
26
27     def get_touclick_image(self, name=‘captcha.png‘):
28         """
29         获取验证码图片
30         :return: 图片对象
31         """
32         top, bottom, left, right = self.get_position()
33         print(‘验证码位置‘, top, bottom, left, right)
34         screenshot = self.get_screenshot()
35         captcha = screenshot.crop((left, top, right, bottom))
36         captcha.save(name)
37         return captcha
38     def __del__(self):
39         self.browser.close()

    (5)利用超级鹰获得需要点触的位置:

  

1 #获取验证码截图
2     image=cg.get_touclick_image()
3     bytes_array=BytesIO()
4     image.save(bytes_array,format=‘PNG‘)
5     #识别验证码
6     result=cg.chaojiying.PostPic(bytes_array.getvalue(),kind)

    (6)根据位置来点触验证码:

    

 1     def getPoint(self,result):
 2         ‘‘‘
 3         获取每个坐标点
 4         :param result:
 5         :return: 返回坐标位置
 6         ‘‘‘
 7         groups=result.get(‘pic_str‘).split(‘|‘)
 8         locations=[[int(number) for number in group.split(‘,‘)] for group in groups]
 9         return locations
10
11     def touch_click_words(self,locations):
12         ‘‘‘
13         点击坐标
14         :param locations:
15         :return:
16         ‘‘‘
17
18         for location in locations:
19             print(location)
20             ActionChains(self.browser).move_to_element_with_offset(self.get_image(), location[0],
21                                                                    location[1]).click().perform()
22             time.sleep(1)

    (7)最后点击提交按钮:

    

1     def submit(self):
2         submit=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,‘geetest_commit‘)))
3         submit.click()
4         time.sleep(5)
5         button=self.wait.until(EC.element_to_be_clickable((By.ID,‘submit-fp-ph‘)))
6         button.click()

  3、基本流程就是这样,爬虫就是为了模拟用户的操作,跟黑客没什么关系,一点也不高大上!!!

  附github代码:selenium完成斗鱼找回密码验证

原文地址:https://www.cnblogs.com/monty12/p/10012126.html

时间: 2024-08-02 06:27:36

python利用selenium库识别点触验证码的相关文章

[python]利用selenium模拟用户操作抓取天猫评论数据

准备: python3.5 安装selenium包 第一种方法: cmd里输pip install selenium,但是经常报错 第二种方法: 下载安装包-cmd进入解压路径-python setup.py install-报错permission denied-右键安全更改报错文件夹权限为完全控制-再次安装成功unknown error: unable to discover open pages-下载chromedriver放在环境变量目录下测试自动打开百度时提示"您使用的是不受支持的命令

python 利用PIL库进行更改图片大小的操作

python 是可以利用PIL库进行更改图片大小的操作的,当然一般情况下是不需要的,但是在一些特殊的利用场合,是需要改变图片的灰度或是大小等的操作的,其实用python更改图片的大小还是蛮简单的,只需要几行代码,有一点可能刚入门的小伙伴们可能不知道PIL库,PIL是一个库的简写,他的真名叫做pillow,因此,需要pip install pillow 用anaconda的话是conda install pillow千万不要pip/conda install PIL咯,下面贴出代码,希望对一些小伙

python利用selenium+requests+beautifulsoup爬取12306火车票信息

在高速发展的时代.乘车出远门是必不可少的,有些查询信息是要收费的.这里打造免费获取火车票信息 想要爬取12306火车票信息,访问12306官方网站,输入出发地,目的地  ,时间  之后点击确定,这是我们打开谷歌浏览器开发者模式找到   https://kyfw.12306.cn/otn/resources/js/framework/station_name.js    这里包含了所有城市的信息和所有城市的缩写字母.想要获取火车票信息  https://kyfw.12306.cn/otn/left

浅谈python中selenium库调动webdriver驱动浏览器的实现原理

最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? 1 from selenium import webdriver 2 driver = webdriver.Chrome() 3 driver.get('http://www.baidu.com') 首先我们来看一下selenium库的结构: 很显然,selenium就是一个软件包,里面有两个一级子包,commo

python 利用selenium爬取百度文库的word文章

今天学习如何使用selenium库来爬取百度文库里面的收费的word文档 from selenium import webdriver from selenium.webdriver.common.keys import Keys from pyquery import PyQuery as pq from selenium.webdriver.support.ui import WebDriverWait from selenium import webdriver import time o

使用python的selenium库刷超星网课

网课很多看不完呀 所以动手做了一个基础的自动答题和下一节的程序 用到了python 3 selenium Chrome 如何自动化Chrome?https://www.cnblogs.com/eternal1025/p/8880245.html 配置好之后上代码 源码下载地址  https://github.com/zlaiyyf/ChaoXIing_seleniumm 时间紧张所以嘛bug就.... coding:utf-8 from selenium import webdriver # 显

[原]Python 使用pytesseract库识别验证码(MAC系统)

1.安装python brew install python 2.安装PIL及图片格式支持 下载http://www.pythonware.com/products/pil/index.htm 解压tar -xzf Imaging-1.1.7.tar.gz 安装PILcd Imaging-1.1.7sudo python setup.py install 安装图片格式支持brew install libpngbrew install jpegbrew install libtiff (注:安装了

Python利用Dlib库进行人脸识别

0.引言 自己在下载dlib官网给的example代码时,一开始不知道怎么使用,在一番摸索之后弄明白怎么使用了: 现分享下 face_detector.py 和 face_landmark_detection.py 这两个py的使用方法: 1.开发环境 python: 3.6.3 dlib: 19.7 2.py文件功能介绍 face_detector.py : 识别出图片文件中一张或多张人脸,并用矩形框框出标识出人脸: face_landmark_detection.py : 在face_det

python下调用pytesseract识别某网站验证码

一.pytesseract介绍 1.pytesseract说明 pytesseract最新版本0.1.6,网址:https://pypi.python.org/pypi/pytesseract Python-tesseract is a wrapper for google's Tesseract-OCR( http://code.google.com/p/tesseract-ocr/ ). It is also useful as astand-alone invocation script