用超级鹰来识别B站图片验证

用超级鹰来识别B站图片验证

"""
1.登录
2.图片验证码(
截取当前页面的图片并且保存下来,裁剪出验证码的区域
使用超级鹰将验证码图片识别
返回汉字对应的坐标点
按照顺序来模拟点击完成每个坐标点)
"""
import random
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ChromeOptions
from chaojiying import Chaojiying_Client
#裁剪模块
from PIL import Image
#去除识别
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option("detach",True)

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://passport.bilibili.com/login?from_spm_id=333.851.top_bar.login')
# #特征识别
script = 'Object.defineProperty(navigator,"webdriver",{get:() => false,});'
driver.execute_script(script)
#登录
def login():
    driver.find_element(By.ID,'login-username').send_keys('123456789')
    driver.find_element(By.ID,'login-passwd').send_keys('1234567891')
    driver.find_element(By.XPATH,'//*[@id="geetest-wrap"]/div/div[5]/a[1]').click()
    time.sleep(2)
def save_img():
    #保存截图
    driver.save_screenshot('page.png')
    #获取验证码图片进行截取
    code_img_elm = driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[6]/div/div')
    #位置
    location = code_img_elm.location
    #大小
    size = code_img_elm.size

    print(location,size)
    #左上角和右下角的坐标
    cangle = (
       int(location['x']) ,int(location['y']), int(location['x']+size['width']),int(location['y']+size['height'])
)
    imge1 = Image.open('./page.png')

    image2 = imge1.crop(cangle)
    image2.save('./code.png')

def narrow_img():
    #进图片缩小2倍
    code = Image.open('./code.png')

    narrow_img = code.resize((135,173))
    narrow_img.save('narrow_img.png')
    print(code.png,narrow_img.size)

def result_img():
    #用超级鹰识别
    chaojiying = Chaojiying_Client('19831060232', '19831060232', '26971')  # 用户中心>>软件ID 生成一个替换 96001
    im = open('./narrow_img.png', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//

    result = chaojiying.PostPic(im, 9004)['pic_str'] # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
    return result

def parse_data(result):
    #将坐标改成超级鹰可以识别的模式
    """

    :return:
    """
    node_list = []

    if '|' in result:
        nlists = result.split('|')
        for i in range(len(nlists)):
            x = int(nlists[i].split(',')[0])
            y = int(nlists[i].split(',')[1])
            xy_list = [x,y]
            node_list.append(xy_list)
    return node_list

def click_image(node_list,code_img_elm):
    """
   用行为链点击

    :param node_list:
    :return:
    """
    for i in node_list:
        x = i[0]*2
        y = i[1]*2
        ac = ActionChains(driver)
        ac.move_to_element_with_offset(driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[6]/div/div'), x, y).click().perform()
        time.sleep(random.random()*5)
        print('点击成功')
    driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[6]/div/div/div[3]/a/div').click()









if __name__ == '__main__':
    login()
    code_img_elm = save_img()
    narrow_img()
    result = result_img()
    node_list = parse_data(result)
    click_image(node_list,code_img_elm)

上一篇:python + ddt数据驱动 之 一个参数


下一篇:AUTOSAR 是什么