Python +Selenium解決圖片驗證碼登錄或注冊問題(推薦)
1. 解決思路
首先要獲得這張驗證碼的圖片,但是該圖片一般都是用的js寫的,不能夠通過url進行下載。
解決方案:截圖然后根據(jù)該圖片的定位和長高,使用工具進行裁剪
裁剪完畢之后,使用工具解析該圖片。
2. 代碼實現(xiàn)
2.1 裁剪出驗證碼圖片
裁剪圖片需要使用 Pillow 庫,進入pip包路徑后輸入安裝命令pip install Pillow:
之前安裝的時候忘記了截圖,只能夠截一張安裝后的圖片了 ╰(:з╰∠)_
安裝完成后,代碼實現(xiàn)方式如下:
#coding=utf-8 from selenium import webdriver import time from PIL import Image from selenium.webdriver.support.wait import WebDriverWait driver = webdriver.Chrome() # 進入該網(wǎng)站 driver.get("http://www2.nmec.org.cn/wangbao/nme/sp/root/account/signup.html") # 能否在5s內(nèi)找到驗證碼元素,能才繼續(xù) if WebDriverWait(driver,5).until(lambda the_driver:the_driver.find_element_by_id("CaptchaImg"), "查找不到該元素"): # 對于一次截屏無法到截到驗證碼的情況,需要滾動一段距離,然后驗證碼的y坐標也應(yīng)該減去這段距離 scroll = 500 js = "document.documentElement.scrollTop='%s'" %scroll driver.execute_script(js) # 截下該網(wǎng)站的圖片 driver.get_screenshot_as_file("E:/Python_selenium_advance/Picture/full.png") # 獲得這個圖片元素 img_ele = driver.find_element_by_id("CaptchaImg") # 得到該元素左上角的 x,y 坐標和右下角的 x,y 坐標 left = img_ele.location.get('x') upper = img_ele.location.get('y') - 500 right = left + img_ele.size.get('width') lower = upper + img_ele.size.get('height') # 打開之前的截圖 img = Image.open("E:/Python_selenium_advance/Picture/full.png") # 對截圖進行裁剪,裁剪的范圍為之前驗證的左上角至右下角范圍 new_img = img.crop((left, upper, right, lower)) # 裁剪完成之后保存到指定路徑 new_img.save("E:/Python_selenium_advance/Picture/croped.png") time.sleep(2) driver.quit() else: print("找不到驗證碼元素")
2.2 使用 圖鑒 商用接口來識別驗證碼
接口介紹網(wǎng)址:http://www.ttshitu.com/docs/python.html#pageTitle
調(diào)用該接口直接使用網(wǎng)頁上的接口文檔就行,代碼如下:
import json import requests import base64 from io import BytesIO from PIL import Image from sys import version_info def base64_api(uname, pwd, softid, img): img = img.convert('RGB') buffered = BytesIO() img.save(buffered, format="JPEG") if version_info.major >= 3: b64 = str(base64.b64encode(buffered.getvalue()), encoding='utf-8') else: b64 = str(base64.b64encode(buffered.getvalue())) data = {"username": uname, "password": pwd, "softid": softid, "image": b64} result = json.loads(requests.post("http://api.ttshitu.com/base64", json=data).text) if result['success']: return result["data"]["result"] else: return result["message"] return ""
將其保存為一個單獨的 analysis_captcha.py ,然后再導(dǎo)入該方法,直接使用即可:
from analysis_captcha import base64_api def analysis_captcha(filename): ''' 使用 圖鑒 商用接口來識別指定位置的驗證碼圖片 :param filename: 驗證碼圖片位置 :return : 驗證碼文本 ''' img_path = filename img = Image.open(img_path) result = base64_api(uname='kaibin', pwd='******', softid='4545454', img=img) return result
驗證碼識別可能會出錯,到時候再點擊驗證碼圖片換一張,然后重來即可。
總結(jié)
以上所述是小編給大家介紹的Python +Selenium實現(xiàn)圖片驗證碼登錄或注冊問題,希望對大家有所幫助!
- 使用 Python 和 Selenium 解決 Cloudflare 驗證碼的問題
- python+selenium行為鏈登錄12306(滑動驗證碼滑塊)
- Python Selenium破解滑塊驗證碼最新版(GEETEST95%以上通過率)
- Selenium+Python 自動化操控登錄界面實例(有簡單驗證碼圖片校驗)
- selenium+python實現(xiàn)1688網(wǎng)站驗證碼圖片的截取功能
- Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能
- Python Selenium Cookie 繞過驗證碼實現(xiàn)登錄示例代碼
- python+selenium識別驗證碼并登錄的示例代碼
- Python爬蟲selenium驗證之中文識別點選+圖片驗證碼案例(最新推薦)
相關(guān)文章
Python實戰(zhàn)之ATM取款機的實現(xiàn)
這篇文章主要為大家詳細介紹了如何利用Python語言模擬實現(xiàn)ATM取款機功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2022-09-09python 爬蟲基本使用——統(tǒng)計杭電oj題目正確率并排序
這篇文章主要介紹了python 爬蟲基本的基本使用,主要利用了Urllib和BeautifulSoup4這兩個庫,配以簡單的實例幫助大家理解,感興趣的朋友可以了解下2020-10-10python不使用for計算兩組、多個矩形兩兩間的iou方式
今天小編就為大家分享一篇python不使用for計算兩組、多個矩形兩兩間的iou方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python中元組的基礎(chǔ)介紹及常用操作總結(jié)
元組是一種不可變序列。元組變量的賦值要在定義時就進行,這就像C語言中的const變量或是C++的引用,定義時賦值之后就不允許有修改。元組存在的意義是:元組在映射中可以作為鍵使用,因為要保證鍵的不變性。元組作為很多內(nèi)置函數(shù)和方法的返回值存在2021-09-09淺析Python中g(shù)etattr和getattribute的調(diào)用
在Python中,getattr和getattribute是兩個用于屬性訪問的重要函數(shù),它們可以在運行時動態(tài)地獲取對象的屬性或自定義屬性訪問行為,下面我們就來學習一下它們的具體用法吧2023-11-11