ライブラリ
import time import ddddocr
ソースコード
# import threading # 导入threading模块 # from Feishu_SendMsg import * # Identification verification code import time import ddddocr interval = 100 * 60 # def delayCall(): # 定义方法 # SendMsg("选题 快快快!!!") # timer=threading.Timer(interval,delayCall) # 每秒运行 # timer.start() # 执行方法 # if __name__ == '__main__': # # t1=threading.Timer(interval,function=delayCall) # 创建定时器 # t1.start() # 开始执行线程 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys # SendMsg("自动填表单") options = webdriver.ChromeOptions() options.add_argument('--enable-automation') options.add_argument('--no-sandbox') options.add_argument('--disable-extensions') options.add_argument('--start-maximized') options.add_argument('--disable-infobars') prefs = {"profile.default_content_setting_values.autocomplete_enabled": 2} options.add_experimental_option("prefs", prefs) # SendMsg("创建 Chrome 浏览器实例") # 创建 Chrome 浏览器实例 browser = webdriver.Chrome(options=options) # SendMsg("打开网页") browser.get('www.tttttttt.com') # SendMsg("找到账号和密码框元素并输入指定字符串") username = browser.find_element("name","username") password = browser.find_element("name","userpass") usercode = browser.find_element("name","usercode") img_verifycode = browser.find_element("id","img_verifycode") # SendMsg("自动填充账号密码") username.send_keys("11111") password.send_keys("11111") verifycodeBase64 = img_verifycode.screenshot_as_base64 ocr = ddddocr.DdddOcr() res = ocr.classification(verifycodeBase64) usercode.send_keys(res) # SendMsg(f"识别并填写验证码: {res}") # SendMsg("提交表单") password.send_keys(Keys.RETURN) # SendMsg("登陆: 提交表单")
ナレッジポイント補足
以下では、この記事で使用されているdddddocrライブラリの関連した使用方法を紹介します
識別 検証コード用の Python ライブラリは多数ありますが、どれも使いにくいです。ddddocr (ブラザー ocr 付き) ライブラリは、検証コードを識別するためのシンプルで実用的なライブラリです。
# を使用することをお勧めします。 #dddddocr.
import os import ddddocr from time import sleep from PIL import Image from selenium import webdriver from selenium.webdriver.common.by import By class GetVerificationCode: def __init__(self): self.res = None url = '要登录的地址' self.driver = webdriver.Chrome() self.driver.maximize_window() # 将浏览器最大化 self.driver.get(url) # 获取验证码信息 def getVerification(self): # 获取当前文件的位置、并获取保存截屏的位置 current_location = os.path.dirname(__file__) screenshot_path = os.path.join(current_location, "..", "VerificationCode") # 截取当前网页并放到自定义目录下,并命名为printscreen,该截图中有我们需要的验证码 sleep(1) self.driver.save_screenshot(screenshot_path + '//' + 'printscreen.png') sleep(1) # 定位验证码 imgelement = self.driver.find_element(By.XPATH, '验证码图片的Xpath定位') # 获取验证码x,y轴坐标 location = imgelement.location # 获取验证码的长宽 size = imgelement.size # 写成我们需要截取的位置坐标 rangle = (int(location['x'] + 430), int(location['y'] + 200), int(location['x'] + size['width'] + 530), int(location['y'] + size['height'] + 250)) # 打开截图 i = Image.open(screenshot_path + '//' + 'printscreen.png') # 使用Image的crop函数,从截图中再次截取我们需要的区域 fimg = i.crop(rangle) fimg = fimg.convert('RGB') # 保存我们截下来的验证码图片,并读取验证码内容 fimg.save(screenshot_path + '//' + 'code.png') ocr = ddddocr.DdddOcr() with open(screenshot_path + '//' + 'code.png', 'rb') as f: img_bytes = f.read() self.res = ocr.classification(img_bytes) print('识别出的验证码为:' + self.res) # 判断验证码错误时的提示信息是否存在 def isElementPresent(self, by, value): try: element = self.driver.find_element(by=by, value=value) except NoSuchElementException: pass # 发生了NoSuchElementException异常,说明页面中未找到该元素,返回False return False else: # 没有发生异常,表示在页面中找到了该元素,返回True return True # 登录 def login(self): self.getVerification() self.driver.find_element(By.XPATH, '用户名输入框Xpath定位').send_keys('用户名') self.driver.find_element(By.XPATH, '密码输入框Xpath定位').send_keys('密码') self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登录按钮Xpath定位').click() sleep(2) isFlag = True while isFlag: try: isPresent = self.isElementPresent(By.XPATH, '验证码错误时的提示信息Xpath定位') if isPresent is True: codeText = self.driver.find_element(By.XPATH, '验证码错误时的提示信息Xpath定位').text if codeText == "验证码不正确": self.getVerification() sleep(2) self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').clear() sleep(1) self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登录按钮Xpath定位').click() sleep(2) tips = self.driver.find_element(By.XPATH, '未输入验证码时的提示信息Xpath定位').text if tips == "请输入验证码": self.getVerification() sleep(2) self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').click() sleep(1) self.driver.find_element(By.XPATH, '验证码输入框Xpath定位').send_keys(self.res) sleep(1) self.driver.find_element(By.XPATH, '登录按钮Xpath定位').click() sleep(2) continue else: print("验证码正确,登录成功!") except NoSuchElementException: pass else: isFlag = False sleep(5) self.driver.quit() if __name__ == '__main__': GetVerificationCode().login()
認識結果
以上がPythonを使ってWebページのコンテンツを取得し、フォーム入力やログインを自動で行う機能を実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

パフォーマンスに対する配列の均一性の影響は二重です。1)均一性により、コンパイラはメモリアクセスを最適化し、パフォーマンスを改善できます。 2)しかし、タイプの多様性を制限し、それが非効率につながる可能性があります。要するに、適切なデータ構造を選択することが重要です。

craftexecutablepythonscripts、次のようになります

numpyarraysarasarebetterfornumeroperations andmulti-dimensionaldata、whilethearraymoduleissuitable forbasic、1)numpyexcelsinperformance and forlargedatasentassandcomplexoperations.2)thearraymuremememory-effictientivearientfa

NumPyArraySareBetterforHeavyNumericalComputing、whilethearrayarayismoreSuitableformemory-constrainedprojectswithsimpledatatypes.1)numpyarraysofferarays andatiledance andpeperancedatasandatassandcomplexoperations.2)thearraymoduleisuleiseightweightandmemememe-ef

ctypesallowsinging andmanipulatingc-stylearraysinpython.1)usectypestointerfacewithclibrariesforperformance.2)createc-stylearraysfornumericalcomputations.3)passarraystocfunctions foreffientientoperations.how、how、becuutiousmorymanagemation、performanceo


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

ホットトピック









