Python学习

随记-点选验证码

2024-03-01  本文已影响0人  DragonFangQy

文字验证码(点击文字)

主要代码

save_image



def save_image(style_value):
    """
        保存图片(目标图片 和 验证图片)
    """
    
    # 获取验证图片
    bg_img_url = re.search(r"""url\("(?P<url>.*?)"\);""", style_value)
    bg_img_width = re.search(r""".*width:.*?(?P<width>.*?)px;""", style_value)
    bg_img_height = re.search(r""".*height:.*?(?P<height>.*?)px;""", style_value)
    bg_img_right = re.search(r""".*right:.*?(?P<right>.*?)px;""", style_value)
    bg_img_top = re.search(r""".*top:.*?(?P<top>.*?)px;""", style_value)
 
    complete_image_path = None
    image_name = ""

    size_info = {}
    if bg_img_url:
        url = bg_img_url.groupdict().get("url", "")
        re_image_name = re.search(r""".*/(?P<image_name>.*?).jpg\?""", url)

        if re_image_name:
            temp_key = "image_name"
            image_name = re_image_name.groupdict().get(temp_key, "")

            size_info.update(bg_img_width.groupdict())
            size_info.update(bg_img_height.groupdict())
            size_info.update(bg_img_right.groupdict())
            size_info.update(bg_img_top.groupdict())

        data = requests.get(url, verify=False)

        complete_image_path = f"{base_path}{image_name}_complete.jpg"

        # 保存验证图片(原图)
        with open(complete_image_path, "wb") as wbf:
            wbf.write(data.content)
    
    image_obj = cv2.imread(complete_image_path)

    height, width, channel = image_obj.shape

    for element_key, element_value in size_info.items():
        size_info[element_key] = int(element_value)

    point = (0, height-size_info["height"], size_info["width"], height)
    image_crop_target = image_obj[height-size_info["height"]:height, 0:size_info["width"]]
    image_crop_verify = image_obj[0:height-size_info["height"], 0:width]
    
    # 保存目标图片
    target_image = f"{image_name}_target.jpg"
    target_image_path = f"{base_path}{target_image}"
    # cv2.imshow("image_crop", image_crop_target)
    # cv2.waitKey(0)
    cv2.imwrite(target_image_path, image_crop_target)
    
    # 保存verify图片
    verify_image = f"{image_name}_verify.jpg"
    verify_image_path = f"{base_path}{verify_image}"
    # cv2.imshow("image_crop", image_crop_target)
    # cv2.waitKey(0)
    cv2.imwrite(verify_image_path, image_crop_verify)

    return complete_image_path, target_image_path, verify_image_path

save_image_crop


def save_image_crop(image_path, width_size = 26):
    """
        裁切目标图片
    """
    
    image_obj = Image.open(image_path)

    width, height = image_obj.width, image_obj.height 

    left_top_point = [] # 左上
    for i in range(0, width, width_size):
        left_top_point.append((i, 0))

    right_bottom_point = [] # 右下
    for i in range(width_size, width, width_size):
        right_bottom_point.append((i, height))
 
    crop_target_list = []
    min_len = min(len(left_top_point), len(right_bottom_point))
    for i in range(min_len):

        temp_list = []
        temp_list.extend(left_top_point[i])
        temp_list.extend(right_bottom_point[i])

        point = tuple(temp_list) 
        image_crop = image_obj.crop(point)
        
        re_image_name = re.search(r""".*/(?P<image_name>.*?).jpg""", image_path)

        image_name = ""
        if re_image_name:
            temp_dict = re_image_name.groupdict()
            image_name = temp_dict["image_name"] 

        image_name_list = image_name.split("_")
        image_name_list.append(str(i))

        new_image_name = "_".join(image_name_list)
        new_image_path = f"{base_path}/{new_image_name}.jpg"

        image_crop.save(new_image_path)
        crop_target_list.append(new_image_path)
    return crop_target_list, height
    

object_detection


def object_detection(complete_image_path):
    det = ddddocr.DdddOcr(det=True, show_ad=False)

    with open(complete_image_path, 'rb') as f:
        image = f.read()

    poses = det.detection(image)
    print(poses) 
        
    im = cv2.imread(complete_image_path)

    for box in poses:
        x1, y1, x2, y2 = box
        im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)

    cv2.imshow("object_detection", im)
    cv2.waitKey(0)

main


if __name__ == "__main__":
    driver = get_driver()  
    
    driver.get('https://www.bixxxxli.com/')

    click_element(driver, """ //div[@class="header-login-entry"] """)
    click_element(driver, """ //div[@class="login-tab-item"] """)
    input_element(driver, """ //div[@class="login-sms-wp__cid"]/../input """, "15266666666")
    click_element(driver, """ //div[@class="login-sms-wp__cid"]/../div[@class="login-sms-send clickable "] """)

    style_value = get_element_value(driver, """ //div[@class="geetest_widget"]//div[@class="geetest_tip_img"] """, "style")
    complete_image_path, target_image_path, verify_image_path = save_image(style_value)

    object_detection(complete_image_path)
    object_detection(target_image_path)
    object_detection(verify_image_path)
    print
    

Source Code


到此结  DragonFangQy 2024.03.02

本博文仅供学习参考之用,不得用于其他任何目的。如有任何内容侵犯到您的隐私或权益,敬请立即联系我,我将及时删除或修正相关内容。感谢您的理解与支持,期待与您共同维护一个友善、尊重知识产权的网络环境。

上一篇下一篇

猜你喜欢

热点阅读