有人倒腾了企业微信那个入口的望传教一下,这里只有笨逼方法
思路
- 用selenium模拟页面交互
- 用开源框架Tesseract OCR实现验证码识别
实现
- 安装这两个框架(linux)
pip install selenium pytesseract
git clone https://github.com/tesseract-ocr/tesseract.git
cd tesseract
./autogen.sh
./configure
make
sudo make install
wget https://hub.fastgit.org/tesseract-ocr/tessdata/raw/master/eng.traineddata
sudo mv eng.traineddata /usr/local/share/tessdata/
- 因为cas的验证码没啥难度,简单的处理下基本就能识别正确
#! captcha.py
from PIL import Image, ImageEnhance
import pytesseract
import re
def reconizeCaptureImg(filepath, save=False):
'''
针对cas验证码的识别方案,进行简单的处理识别
'''
img = Image.open(filepath)
# 采用简单的灰度处理,直接根据灰度筛除干扰直线
img = img.convert('L')
threshold_u = 200
threshold_d = 15
table = []
for i in range(256):
if i < threshold_u and i > threshold_d:
table.append(0)
else:
table.append(1)
img = img.point(table, '1')
# 保持处理图片
if save:
img.save('captcha-handled.png')
# 使用tesseract-orc来识别
code = pytesseract.image_to_string(img).strip()
# 打印识别出的验证码,用于调试正则
# print(code)
# 直接正则防止识别出了其他东西(因为只有字母数字)
b=''
for i in code:
pattern = re.compile(r'[a-zA-Z0-9]')
m = pattern.search(i)
if m != None:
b+=i
# 返回结果
return b
附上一点测试代码
#! captcha_test.py
from captcha import reconizeCaptureImg
def test():
count, right_count = 0, 0
import os
path=r'./img'
if os.path.exists(path):
files=os.listdir(path)
for file in files:
count += 1
ans = file.split('.')[0]
res = reconizeCaptureImg(os.path.join(path, file))
if ans == res:
right_count += 1
else:
print('except: {}, got: {}'.format(ans,res))
print('right rate: {}/{}'.format(right_count,count))
else:
print('this path not exist')
# 测试程序
test()
- 写个配置,再用selenium点点点就完事了
#! config.py
username='username'
password='password'
#! jksb.py
from selenium import webdriver
from captcha import reconizeCaptureImg
import config
driver = webdriver.Chrome()
# 登陆统一门户
driver.get('https://portal.sysu.edu.cn/#/login')
driver.find_element_by_xpath('//*[@id="root"]/span/div/div[2]/div/div[1]/div[2]/div/div/div/button').click()
flag = False
# 最多尝试10次,防止程序因为不名原因变成了dos
for i in range(10):
# 输入用户名、密码
driver.find_element_by_xpath('//*[@id="username"]').send_keys(config.username)
driver.find_element_by_xpath('//*[@id="password"]').send_keys(config.password)
# 保存验证码图片
tmp_file = '/tmp/capture.png'
driver.find_element_by_xpath('//*[@id="captchaImg"]').screenshot(tmp_file)
# 识别并输入验证码
captcha = reconizeCaptureImg(tmp_file)
driver.find_element_by_xpath('//*[@id="captcha"]').send_keys(captcha)
# 点击登陆
driver.find_element_by_xpath('//*[@id="fm1"]/section[2]/input[4]').click()
driver.implicitly_wait(5)
try: # 如果有错误标语,就重新填过,否则就算登陆成功
driver.find_element_by_css_selector('.alert-danger').click()
continue
except:
flag = True
break
if not flag:
# (optional)按需自行放个hook通知一下吧
# from wechatTool import sendMsg
# sendMsg(title='Robot Failed',content='自动健康申报失败')
exit()
# 点击健康申报
driver.find_element_by_xpath('//*[@id="topSideLink"]/div[1]/div[3]/div[1]/div[2]/div[1]/div/div/div/div/div/div/div/div/div/ul/li[1]/div[1]').click()
# TODO: 健康申报又双叒叕崩了,后面有需要的自行加上两个点击事件吧,反正只用点两下确认
# driver.find_element_by_xpath('').click()
# driver.find_element_by_xpath('').click()
# TODO: (optional)还需要一个成功判断,防止提交的时候崩了,虽说大概了不会吧..
# 关闭浏览器
driver.quit()
半成品,TODO那里自行处理吧 4. 设置个定时任务(linux)
|