# 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 识别验证码自动登陆的更多相关文章

  1. python识别验证码——PIL,pytesser,pytesseract的安装

    1.使用Python识别验证码需要安装Python的图像处理模块(PIL.pytesser.pytesseract) (安装过程需要pip,在我的Python中已经安装pip了,pip的安装就不在赘述 ...

  2. Python爬虫入门教程 60-100 python识别验证码,阿里、腾讯、百度、聚合数据等大公司都这么干

    常见验证码 之前的博客中已经解决了一些常见验证码的问题,但是验证码是层出不穷的,目前解决验证码除了通过常规手段解决以外,还可以通过人工智能领域的深度学习去解决 深度学习?! 无疑对爬虫coder提高了 ...

  3. python识别验证码——一般的数字加字母验证码识别

    1.验证码的识别是有针对性的,不同的系统.应用的验证码区别有大有小,只要处理好图片,利用好pytesseract,一般的验证码都可以识别 2.我在识别验证码的路上走了很多弯路,重点应该放在怎么把图片处 ...

  4. 使用python识别验证码

    公司的登录注册等操作有验证码,测试环境可以让开发屏蔽掉验证码,但是如果到线上的话就要想办法识别验证码或必过验证码了. 识别验证码主要分为三部分,一.对验证码进行二值化.二.将二值化后的图片分割.三.进 ...

  5. python识别验证码

    1.tesseract-ocr安装 tesseract-ocr windows下载地址 http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr- ...

  6. Python识别验证码,基于Tesseract实现图片文字识别

    一.简介 Tesseract是一个开源的文本识别[OCR]引擎,可通过Apache 2.0许可获得.它可以直接使用,或者使用API从图像中提取打印的文本,支持多种语言.该软件包包含一个ORC引擎[li ...

  7. python 识别验证码

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...

  8. 爬虫实战【10】利用Selenium自动登陆京东签到领金币

    今天我们来讲一下如何通过python来实现自动登陆京东,以及签到领取金币. 如何自动登陆京东? 我们先来看一下京东的登陆页面,如下图所示: [插入图片,登陆页面] 登陆框就是右面这一个框框了,但是目前 ...

  9. [转载]python实现带验证码网站的自动登陆

        原文地址:python实现带验证码网站的自动登陆作者:TERRY-V 早听说用python做网络爬虫非常方便,正好这几天单位也有这样的需求,需要登陆XX网站下载部分文档,于是自己亲身试验了一番 ...

随机推荐

  1. ajax请求的原生js实现

    我们使用ajax请求一般都用的jQuery, axios封装好了的api, 那么如果只能用原生js, 我们该如何操作了? 上代码. 我们在同目录下写好一个json文件(data.json)用于请求测试 ...

  2. shell脚本之用户管理

    #!/usr/bin/env bash ############################### # 脚本名称 : userManager.sh # # 脚本功能 : 账号管理 # # 脚本参数 ...

  3. Add cast to是什么意思

    eclipse中是强制类型转换的意思.

  4. MySql查询结果按照指定顺序排序

    Mysql这功能做的很好用啊!! 让查询结果按照指定顺序排序 --------------------------------------------------------------------- ...

  5. javascript基础总结之实例(二)

    div的显示和隐藏 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  6. Shiro学习(3)授权

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permission).角 ...

  7. Flex布局(一)

    Flex Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为Flex布局.display: flex;, 行内元 ...

  8. 【LeetCode 20】有效的括号

    题目链接 [题解] 一道傻逼括号匹配题 [代码] class Solution { public: bool isValid(string s) { vector<char> v; int ...

  9. JAVA调用R脚本 windwos路径下

    RConnection c = new RConnection();// REXP x = c.eval("source('D:\\\\jiaoben\\\\RJava_test.R',en ...

  10. 剑指offer——46数字序列中某一位的数字

    题目: 数字以0123456789101112131415…的格式序列化到一个字符序列中.在这个序列中,第5位(从0开始计数)是5,第13位是1,第19位是4,等等.请写一个函数,求任意第n位对应的数 ...