日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

說明

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

基本步驟

  • selenium打開對應網(wǎng)站,并進行截圖
  • 將圖片截取出對應驗證碼所在圖片
  • 通過超級鷹識別出要點擊的圖片坐標
  • 點擊相應圖片坐標
  • 輸入用戶名和密碼
  • 點擊登錄,完成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()

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。