利用百度AI开放平台识别干部培训网登录验证码

先说能干啥能干啥,看视频。

MyVideo_2

个人测试,百度AI平台验证码识别率还是很高的。下面是视频程序的源代码,欢迎跟大家进行交流。

import datetime
import json
from aip import AipOcr
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time

def get_file_content(filePath):  #获取图片地址
    with open(filePath, 'rb') as fp:
        return fp.read()



def pic_shibie():   #识别图片里的验证码
    #百度AI平台注册后,给你的分配的授权码(免费)
    APP_ID = '**********'
    API_KEY = '***********'
    SECRET_KEY = '************'

    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    image = get_file_content('000.png')
    s = client.basicGeneral(image)
    s = json.dumps(s)
    s = json.loads(s)
    word = s["words_result"]

    code = word[0]["words"]

    return code

def pic_caijian(loc,size):  #裁剪图片 用于识别
    img = Image.open('222.png')
    x1 = int(loc['x'])-20
    y1 = int(loc['y'])-50
    x2 = x1 + int(size["width"])+150
    y2 = y1 +int(size["height"])+150

    cropped = img.crop((x1, y1, x2, y2))  # (left, upper, right, lower)
    cropped.save("000.png")

#模拟浏览器登录---学习


username = input("请输入登录账号(手机号):")
password = input("请输入登录密码:")
#
# print("请在弹出的网页中输入验证码!")
# time.sleep(2)
# 登录



url = "https://www.sqgj.gov.cn/"
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get(url)
driver.maximize_window()
driver.find_element_by_xpath('//*[@id="new_mobilePhoneLogin"]').send_keys(username)
driver.find_element_by_xpath('//*[@id="new_passWordLogin"]').send_keys(password)
pic = driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/div[1]/div/div[1]/div[2]/ul[1]/li[3]/div/span/img')
loc = pic.location
size = pic.size
print(loc)
print(size)

try:
    picture_url=driver.save_screenshot('222.png')
    print("%s :截图成功!!!" % picture_url)
except BaseException as msg:
    print("%s :截图失败!!!" % msg)
    date = str(datetime.date.today())
    year = date[:4]
    if year != '2021':
        exit()
pic_caijian(loc, size)
code = pic_shibie()
driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/div[1]/div/div[1]/div[2]/ul[1]/li[3]/div/input').send_keys(code)
time.sleep(3)

driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/div[1]/div/div[1]/div[2]/ul[1]/li[4]/button').click()

time.sleep(5)
xuexicishu = 0
# kecheng = driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div/div[2]/div[3]/ul/li/ul[1]/li[1]')
# total_kecheng = len(kecheng)
while True:
    driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[2]/div[3]/ul/li[1]/ul[1]/li[1]').click()

    time.sleep(5)
    xuexicishu = xuexicishu + 1
    windows = driver.window_handles
    driver.switch_to.window(windows[1])
    time.sleep(5)
    # 选择学习视频
    count = driver.find_elements_by_xpath(
        '/html/body/div[2]/div[2]/div[3]/div/div/div[2]/ul/li[@class !="hasStudy"]/span[2]')

    for i in range(0, len(count)):
        time.sleep(2)
        count = driver.find_elements_by_xpath(
            '/html/body/div[2]/div[2]/div[3]/div/div/div[2]/ul/li[@class !="hasStudy"]/span[2]')
        print(f"这是第{i+1}次学习")

        count[0].click()

        time.sleep(10)

        # 切换到播放窗口
        windows = driver.window_handles
        driver.switch_to.window(windows[2])

        # 确认课程学习时间
        keshi = driver.find_element_by_xpath('//*[@id="study"]/span[1]/strong').text
        print(f"本科总课时{keshi}")
        yixueshijian = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/span[2]/strong').text
        if int(keshi) - int(yixueshijian) > 0:
            study_time = int(keshi) - int(yixueshijian)
        else:
            study_time = 1
        print(f"本次需要学习{study_time}分钟")
        iframe = driver.find_element_by_xpath('//*[@id="playResourceFrame"]')

        driver.switch_to_frame(iframe)
        time.sleep(3)
        driver.find_element_by_xpath('/html/body/div[2]/div/div[3]/div[2]').click()
        time.sleep(study_time * 60 + 63)

        # 点击退出

        time.sleep(3)
        driver.find_element_by_xpath('//*[@id="exit"]').click()
        time.sleep(3)
        driver.switch_to_alert().accept()
        time.sleep(3)
        driver.switch_to_alert().accept()
        time.sleep(5)
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        driver.refresh()
        time.sleep(6)
    driver.close()
    windows = driver.window_handles
    driver.switch_to.window(windows[0])
    driver.refresh()
    time.sleep(5)

    print("干部培训网,完成第" + str(xuexicishu) + "次学习")

不懂代码怎么办,分享一下生成好的EXE程序,再激发一下你的学习兴趣。

链接:https://pan.baidu.com/s/1kRgxuqPBIix2jCdsI21EFQ
提取码:4444

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值