English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
業務上の必要から、ウェブサイトへのログインには認証コードが必要です。最初は認証コード認識について研究しましたが、必要な認証コードを取得できませんでした。金曜日のこと、そのことに思い出しました。昨日無事に解決しました。
本日の主題:
pythonバージョン:3.4.3
必要なライブラリ:PIL、selenium、tesseract
以下のコードを先に示します:
#coding:utf-8 import subprocess from PIL import Image from PIL import ImageOps from selenium import webdriver import time,os,sys def cleanImage(imagePath): image = Image.open(imagePath) #画像を開きます image = image.point(lambda x: 0 if x<143 else 255) #画像上の各ピクセルを処理し、画像上の各点が「黒か白か」のどちらかになります borderImage = ImageOps.expand(image,border=20,fill='white') borderImage.save(imagePath) def getAuthCode(driver, url="http://localhost/"): captchaUrl = url + "common"/random" driver.get(captchaUrl) time.sleep(0.5") driver.save_screenshot("captcha.jpg") #スクリーンショットを撮影し、画像を保存 #urlretrieve(captchaUrl, "captcha.jpg") time.sleep(0.5") cleanImage("captcha.jpg") p = subprocess.Popen(["tesseract", "captcha.jpg", "captcha"], stdout=\ subprocess.PIPE,stderr=subprocess.PIPE) p.wait() f = open("captcha.txt", "r") #クリア任何空白字符 captchaResponse = f.read().replace(" ", "").replace("\n", "") print("Captcha solution attempt: " + captchaResponse if len(captchaResponse) == 4: return captchaResponse else: return False def withoutCookieLogin(url="http://org.cfu666.com/"): driver = webdriver.Chrome() driver.maximize_window() driver.get(url) while True: authCode = getAuthCode(driver, url) if authCode: driver.back() driver.find_element_by_xpath("//input[@id='orgCode' and @name='orgCode']".clear() driver.find_element_by_xpath("//input[@id='orgCode' and @name='orgCode']".send_keys("orgCode") driver.find_element_by_xpath("//input[@id='account' and @name='username']".clear() driver.find_element_by_xpath("//input[@id='account' and @name='username']".send_keys("username") driver.find_element_by_xpath("//input[@type='password' and @name='password']".clear() driver.find_element_by_xpath("//input[@type='password' and @name='password']".send_keys("password") driver.find_element_by_xpath("//input[@type='text' and @name='authCode']".send_keys(authCode) driver.find_element_by_xpath("//button[@type='submit']".click() try: time.sleep(3") driver.find_element_by_xpath("//*[@id='side-menu"]/li[2]/ul/li/a".click() return driver except: print("authCode Error:", authCode) driver.refresh() return driver driver = withoutCookieLogin("http://localhost/) driver.get("http://localhost/enterprise/add/)
どのようにして必要な验证码を取得するか
验证码を取得する道のりで、私は多くのハンデを乗り越え、多くの記事を読みました。多くの記事は验证码の認識方法を教えていますが、どのように現在必要な验证码画像を取得するかは説明されていません。
私の処理方法は:
1.まずはseleniumで必要なログインページのURLを開きます1
2.検証要素を通じて验证码のURLを取得2(実際には右クリックして新しいウィンドウを開くのが最も簡単です)
3:URL1ページにURLを入力2URLにアクセス2ページに戻り、验证码ページをスクリーンショットで保存
4:验证码を処理し、验证码の文字列を取得します。その後、ブラウザの戻るボタンをクリックしてURLに戻ります1ログインページ
5:ログインに必要な情報と验证码を入力
6:ログインをクリック
7:ログイン後のページを確認し、成功しているかどうかを判断します。不成功の場合は、再び1-7の操作。
会社の情報を保護するため、このページは私のローカルで構築したサービスです。私は伯乐在线の登録ページでこの验证码の取得方法をテストしました。通過できます。(この验证码の処理方法は、验证码の背景がピクセルポイントの場合に限り、横線がある場合は追加で処理が必要です。)
これで本文のすべてが終わりました。皆様の学習に役立てば幸いです。また、呐喊教程を多くのサポートをお願いします。
声明:本文の内容はインターネットから取得しており、著作権者に帰属します。インターネットユーザーにより自発的に貢献し、自己でアップロードされたものであり、本サイトは所有権を有しておらず、人工的な編集は行われていません。著作権侵害の疑いがある場合は、メールを送信して:notice#oldtoolbag.com(メールを送信する際には、#を@に変更してください。報告を行い、関連する証拠を提供してください。一旦確認がとりたいとされると、本サイトは即座に侵害疑いのコンテンツを削除します。)