日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

基于selenium实现12306的登录操作(图形验证码识别)

發布時間:2025/1/21 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于selenium实现12306的登录操作(图形验证码识别) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

說明

12306 會有如下一個圖形驗證碼識別點擊,所以必須得先點擊正確圖片,才能繼續進行操作。

基本步驟

  • selenium打開對應網站,并進行截圖
  • 將圖片截取出對應驗證碼所在圖片
  • 通過超級鷹識別出要點擊的圖片坐標
  • 點擊相應圖片坐標
  • 輸入用戶名和密碼
  • 點擊登錄,完成12306的登錄
  • 演示操作

    代碼

    # !/user/bin/env python # -*- coding: utf-8 -*-from chaojiying import get_code from selenium import webdriver from time import sleep from PIL import Image from selenium.webdriver import ActionChainsif __name__ == '__main__':browser = webdriver.Chrome('./chromedriver.exe')browser.get('https://kyfw.12306.cn/otn/login/init')# 因為驗證碼圖片是不固定的,所以需要對頁面進行截取sleep(3)# 瀏覽器截圖browser.save_screenshot('./page.png')# 確認驗證碼圖片的位置code_img_ele = browser.find_element_by_xpath('/html/body/div[6]/div/form/div/ul[2]/li[4]/div/div/div[3]/img')print(code_img_ele)# 獲得驗證碼圖片的位置location = code_img_ele.locationprint(location)# 獲得驗證碼圖片的長和寬size = code_img_ele.sizepos_tuple = (int(location['x']),int(location['y']),int(location['x'] + size['width']),int(location['y'] + size['height']),)# 圖片裁剪i = Image.open('./page.png')frame = i.crop(pos_tuple)frame.save('./code.png')# 使用超級鷹進行驗證碼識別res = get_code('./code.png', '9004')['pic_str']print('圖片地址', res)# 地址切割point_list = res.split('|')new_list = []for point in point_list:xy = point.split(',')temp = [int(xy[0]), int(xy[1])]new_list.append(temp)print(new_list)for point in new_list:x = point[0]y = point[1]ActionChains(browser).move_to_element_with_offset(code_img_ele, x, y).click().perform()sleep(0.1)account = {'username': '您自己的用戶名(自行修改)','password': '您自己的密碼(自行修改)'}username_input = browser.find_element_by_id('username')password_input = browser.find_element_by_id('password')username_input.send_keys(account['username'])password_input.send_keys(account['password'])submit_btn = browser.find_element_by_id('loginSub')submit_btn.click()sleep(60)browser.quit()

    總結

    以上是生活随笔為你收集整理的基于selenium实现12306的登录操作(图形验证码识别)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。