最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)獲取網(wǎng)頁內容及自動填表單與登錄功能

 更新時間:2023年03月22日 14:07:03   作者:極客柒  
這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)模擬瀏覽器啟動,獲取網(wǎng)頁內容、自動填表單、自動登錄、自動過驗證碼等功能,需要的可以參考一下

食用前準備

python 3.10.10 #二維碼的庫ddddocr 需要

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()  # 執(zhí)行方法
    
# if __name__ == '__main__':  #
#     t1=threading.Timer(interval,function=delayCall)  # 創(chuàng)建定時器
#     t1.start()  # 開始執(zhí)行線程


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("創(chuàng)建 Chrome 瀏覽器實例")
# 創(chuàng)建 Chrome 瀏覽器實例
browser = webdriver.Chrome(options=options)

# SendMsg("打開網(wǎng)頁")
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("登陸: 提交表單")

知識點補充

下面為大家介紹一下文中用到的ddddocr庫的相關使用吧

識別驗證碼的python 庫有很多,用起來也并不簡單,ddddocr (帶帶弟弟ocr)庫是一個簡單實用的識別驗證碼的庫,推薦給大家

ddddocr具體使用方法

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")
        # 截取當前網(wǎng)頁并放到自定義目錄下,并命名為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函數(shù),從截圖中再次截取我們需要的區(qū)域
        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
            # 發(fā)生了NoSuchElementException異常,說明頁面中未找到該元素,返回False
            return False
        else:
            # 沒有發(fā)生異常,表示在頁面中找到了該元素,返回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實現(xiàn)獲取網(wǎng)頁內容及自動填表單與登錄功能的文章就介紹到這了,更多相關Python獲取網(wǎng)頁內容內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python實現(xiàn)按逗號分隔列表的方法

    Python實現(xiàn)按逗號分隔列表的方法

    今天小編就為大家分享一篇Python實現(xiàn)按逗號分隔列表的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 詳解python實現(xiàn)可視化的MD5、sha256哈希加密小工具

    詳解python實現(xiàn)可視化的MD5、sha256哈希加密小工具

    這篇文章主要介紹了詳解python實現(xiàn)可視化的MD5、sha256哈希加密小工具,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 使用PyAudio進行音頻處理的詳細指南

    使用PyAudio進行音頻處理的詳細指南

    PyAudio是一個基于PortAudio庫的Python綁定,它允許Python程序直接訪問和操作音頻設備,下面我們就來學習一下PyAudio處理音頻的具體操作吧
    2025-02-02
  • python中adb有什么功能

    python中adb有什么功能

    在本篇文章里小編給大家分享的是關于python中adb有功能的知識點總結,有需要的可以跟著學習下。
    2020-06-06
  • 通過Jython調用Python腳本的實現(xiàn)方法

    通過Jython調用Python腳本的實現(xiàn)方法

    Jython 是 Python 的純 Java 實現(xiàn)。她無縫地結合了 Java 類與 Python,使用戶能以 Python 語言的語法編寫在 Java 虛擬機上運行的 軟件,本文重點給大家介紹通過Jython調用Python腳本的實現(xiàn)方法,一起看看吧
    2021-06-06
  • PyTorch張量拼接、切分、索引的實現(xiàn)

    PyTorch張量拼接、切分、索引的實現(xiàn)

    在學習深度學習的過程中,遇到的第一個概念就是張量,張量在pytorch中的計算十分重要,本文主要介紹了PyTorch張量拼接、切分、索引的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • python web基礎之加載靜態(tài)文件實例

    python web基礎之加載靜態(tài)文件實例

    下面小編就為大家分享一篇python web基礎之加載靜態(tài)文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • python3.9安裝RobotFramework的簡單教程

    python3.9安裝RobotFramework的簡單教程

    python3.9安裝RobotFramework,不同于python2.7和python3.6,使用這兩個版本安裝會出現(xiàn)問題,因為我安裝遇到問題發(fā)現(xiàn)沒有最新的教程,所以打算自己寫一個,同時下面會記錄安裝步驟及使用的方法會出現(xiàn)的一些問題,對python3.9安裝RobotFramework感興趣的朋友一起看看吧
    2023-01-01
  • Python實現(xiàn)softmax反向傳播的示例代碼

    Python實現(xiàn)softmax反向傳播的示例代碼

    這篇文章主要為大家詳細介紹了Python實現(xiàn)softmax反向傳播的相關資料,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的可以了解一下
    2023-04-04
  • python基礎知識之索引與切片詳解

    python基礎知識之索引與切片詳解

    在python的學習過程,有些同學對索引和切換會感到困惑,今天我們就來弄清楚它,下面這篇文章主要給大家介紹了關于python基礎知識之索引與切片的相關資料,需要的朋友可以參考下
    2022-05-05

最新評論

万载县| 突泉县| 灵台县| 台安县| 景泰县| 绍兴县| 铁力市| 阳谷县| 汾阳市| 武陟县| 常州市| 临夏县| 栾川县| 兴业县| 河北省| 文成县| 恩施市| 黎川县| 临朐县| 武强县| 咸宁市| 页游| 福贡县| 贵溪市| 罗源县| 巨野县| 镇坪县| 湾仔区| 永康市| 屯昌县| 合作市| 易门县| 牡丹江市| 乌拉特前旗| 乐平市| 永安市| 湖北省| 方城县| 南安市| 同江市| 东乡族自治县|