python 识别验证码自动登陆
# python 3.5.0
# 通过Chrom浏览器访问发起请求
# 需要对应版本的Chrom和chromdriver
# 作者:linyouyi from selenium import webdriver
# 引入Keys类包 发起键盘操作
from selenium.webdriver.common.keys import Keys
import threading
import time
import random
import requests
import eventlet
import _thread
from io import BytesIO
from PIL import Image
from PIL import ImageEnhance
import pytesseract
import re
pytesseract.pytesseract.tesseract_cmd = 'D:\\Program Files\\Tesseract-OCR\\tesseract.exe'
tessdata_dir_config = '--tessdata-dir "D:\\Program Files\\Tesseract-OCR\\tessdata"' def chrome():
print("启动第一个线程==============================")
chromeOptions = webdriver.ChromeOptions()
#chromeOptions.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"')
chrome_driver="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
#chromeOptions.add_argument("--headless")
chromeOptions.add_argument("--disable-gpu")
#下面两行是禁止加载图片,提高速度
#prefs = {"profile.managed_default_content_settings.images":2}
#chromeOptions.add_experimental_option("prefs",prefs) driver = webdriver.Chrome(chrome_options=chromeOptions,executable_path=chrome_driver)
return driver def read_file(filedir):
'''读取链接文件'''
file = open(filedir,'r')
return file def send_massage(filedir):
'''一次返回一个链接'''
file = read_file(filedir)
for line in file:
# 生成器,一次返回一项
yield line
file.close() def binaryzation(code_image,value):
'''二值化处理'''
#转换成灰度
im = code_image.convert('L')
#对比度增强
im = ImageEnhance.Contrast(im)
im = im.enhance(1)
#锐度增强
#im=ImageEnhance.Sharpness(im)
#im=im.enhance(3.0)
#色度增强
#im=ImageEnhance.Color(im)
#im=im.enhance(3.0)
#亮度增强
#im=ImageEnhance.Brightness(im)
#im=im.enhance(2.0) table = []
for y in range(256):
if y < value:
table.append(0)
else:
table.append(1)
im = im.point(table,'')
return im def discern(code_img):
'''识别验证码'''
try:
im = binaryzation(code_img,127)
code = pytesseract.image_to_string(im)
# 保留数字和字母
code = re.sub("\W", "", code)
if code == '':
return ""
else:
return code
except:
return "识别验证码失败!!!" def call_link(filedir):
'''在所有input填入手机号码,获取验证码图片,识别完输入验证码'''
driver = chrome()
link = send_massage(filedir)
for link in link:
print(link)
try:
# 超时则跳过
eventlet.monkey_patch()
with eventlet.Timeout(100,False):
# 访问链接
driver.get(link)
# 最多等待10秒
driver.implicitly_wait(10)
button = driver.find_elements_by_xpath('//button')
span = driver.find_elements_by_xpath('//span')
inp = driver.find_elements_by_xpath('//div//input')
'''# 所有input都填上手机号码
for aa in inp:
try:
aa.send_keys('00000000000')
time.sleep(random.randint(1,2))
except:
print("########")'''
time.sleep(5)
# 获取所有图片标签
images = driver.find_elements_by_xpath('//img')
for img in images:
img_link = img.get_attribute("src")
if ("captcha" in img_link):
print(img_link)
# 获取验证码在画布中的位置x,y轴坐标
img_location = img.location
# 获取验证码大小
img_size = img.size
# 截取的是整个屏幕
code_img = driver.get_screenshot_as_png()
# 截图保存
#driver.get_screenshot_as_file('D:\\pythontest\\duanxinhongzha\\aa.png')
code_img = Image.open(BytesIO(code_img))
# 使用Image的crop函数,从截图中再次截取我们需要的验证码所在区域
code_img = code_img.crop((img_location['x'],img_location['y'],int(img_location['x'] + img_size['width']),int(img_location['y'] + img_size['height'])))
# 图片放大两倍
code_img = code_img.resize((img_size['width'] * 2,img_size['height'] * 2))
#code_img.save('D:\\pythontest\\duanxinhongzha\\aa.png')
print("验证码所在区域大小为:", code_img.size)
# 把识别的验证码填入,如果识别不出来择忽略错误
code_num = discern(code_img)
print(code_num)
# 根据条件输入验证码,不符合条件的input都填上手机号码
for inp_num in inp:
try:
if ("captcha" in inp_num.get_attribute('id').lower() ):
inp_num.send_keys(code_num)
elif ("ode" in inp_num.get_attribute('id').lower()):
inp_num.send_keys(code_num)
elif ("captcha" in inp_num.get_attribute('name').lower()):
inp_num.send_keys(code_num)
else:
inp_num.send_keys('')
time.sleep(random.randint(1,2))
except:
print("########") # 如果按钮是a标签形式,则获取然后点击
try:
driver.find_element_by_partial_link_text("获取").click()
except:
print("a标签失败")
# 如果按钮是button标签形式,则获取然后点击
try: for button in button:
if ("获取" in button.text or "发送" in button.text or "码" in button.text):
button.click()
except:
print("button失败!!!")
# 如果按钮是span标签形式,则获取然后点击
try:
for span in span:
if ("获取" in span.text or "发送" in span.text or "码" in span.text):
span.click()
except:
print("span失败!!!")
# 如果按钮是input标签形式,则获取然后点击
try:
for inp in inp:
if ("获取" in inp.get_attribute("value") or "发送" in inp.get_attribute("value") or "码" in inp.get_attribute("value")):
inp.click()
except:
print("input失败!!!")
#driver.find_element_by_partial_link_text(str(u"获取").encode('utf-8')).send_keys(Keys.ENTER)
#driver.find_element_by_partial_link_text('获取').find_element().click()
print("短信发送完毕!!!!")
time.sleep(5) except:
print("获取文本失败!!!")
driver.quit() if __name__ == '__main__':
#t1 = threading.Thread(target=query_register)
#t2 = threading.Thread(target=button)
t3 = threading.Thread(target=call_link('D:\pythontest\lianjie1.txt')) #t1.start()
#t2.start()
t3.start()
python 识别验证码自动登陆的更多相关文章
- python识别验证码——PIL,pytesser,pytesseract的安装
1.使用Python识别验证码需要安装Python的图像处理模块(PIL.pytesser.pytesseract) (安装过程需要pip,在我的Python中已经安装pip了,pip的安装就不在赘述 ...
- Python爬虫入门教程 60-100 python识别验证码,阿里、腾讯、百度、聚合数据等大公司都这么干
常见验证码 之前的博客中已经解决了一些常见验证码的问题,但是验证码是层出不穷的,目前解决验证码除了通过常规手段解决以外,还可以通过人工智能领域的深度学习去解决 深度学习?! 无疑对爬虫coder提高了 ...
- python识别验证码——一般的数字加字母验证码识别
1.验证码的识别是有针对性的,不同的系统.应用的验证码区别有大有小,只要处理好图片,利用好pytesseract,一般的验证码都可以识别 2.我在识别验证码的路上走了很多弯路,重点应该放在怎么把图片处 ...
- 使用python识别验证码
公司的登录注册等操作有验证码,测试环境可以让开发屏蔽掉验证码,但是如果到线上的话就要想办法识别验证码或必过验证码了. 识别验证码主要分为三部分,一.对验证码进行二值化.二.将二值化后的图片分割.三.进 ...
- python识别验证码
1.tesseract-ocr安装 tesseract-ocr windows下载地址 http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr- ...
- Python识别验证码,基于Tesseract实现图片文字识别
一.简介 Tesseract是一个开源的文本识别[OCR]引擎,可通过Apache 2.0许可获得.它可以直接使用,或者使用API从图像中提取打印的文本,支持多种语言.该软件包包含一个ORC引擎[li ...
- python 识别验证码
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...
- 爬虫实战【10】利用Selenium自动登陆京东签到领金币
今天我们来讲一下如何通过python来实现自动登陆京东,以及签到领取金币. 如何自动登陆京东? 我们先来看一下京东的登陆页面,如下图所示: [插入图片,登陆页面] 登陆框就是右面这一个框框了,但是目前 ...
- [转载]python实现带验证码网站的自动登陆
原文地址:python实现带验证码网站的自动登陆作者:TERRY-V 早听说用python做网络爬虫非常方便,正好这几天单位也有这样的需求,需要登陆XX网站下载部分文档,于是自己亲身试验了一番 ...
随机推荐
- 转:C++ 智能指针的正确使用方式
转:https://www.cyhone.com/articles/right-way-to-use-cpp-smart-pointer/#comments C++11 中推出了三种智能指针,uniq ...
- 修改Centos中的ll命令(以 K 为单位显示文件大小)
修改CentOS ll命令:以K 为单位显示文件大小 1.编辑 .bashrc 文件:vim /root/.bashrc 2.找到 alias ll 行修改为(如果没有直接添加该行):alias ll ...
- php-验证码类-PDO类-缩略图类
Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...
- 百度API实例——google地图数据转化为百度地图数据
前段时间做的项目前端都是用Google地图,最近在一个地方需要用到百度地图,因为不同地图都有自己的处理,同一个经纬度在不同地图上显示的位置并不相同,因此,要把以前的数据直接拿过来用需要做一个转换.查阅 ...
- ajax请求是的动画实现
ajax请求是的动画实现 ajax是基于XMLHttpRequest对象封装,ajax的具体属性就有: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type S ...
- 实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历
继承Comparator接口,重写compare()方法 import java.util.ArrayList; import java.util.Arrays; import java.util.C ...
- js基础关系运算符
js基础关系运算符 == 是否相等(只检查值) x=5,y='-5';x==y true === 是否全等(检查值和数据类型) x=5,y='-5';x===y false != 是否不等于 5!=8 ...
- mac 安装brew mac安装expect mac一键登录服务器脚本
mac 安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...
- 在一台机上搭建多个MYSQL并设置主从
安装 cd /usr/local/src/ -linux2.-x86_64.tar.gz -linux2.-x86_64 /usr/local/mysql grep mysql /etc/passwd ...
- 拾遗:编译安装 vim
在非 Gentoo/Funtoo 系的 Linux 发行版上,软件仓库里的 vim 包可能没有加入 python 特性支持,会造成部分插件无法正常使用 一.下载 vim 源码包 ftp://ftp.v ...