Python怎么实现获取网页内容及自动填表单与登录功能
python
库
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("登陆: 提交表单")
登录后复制
知识点补充
下面为大家介绍一下文中用到的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") # 截取当前网页并放到自定义目录下,并命名为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怎么实现获取网页内容及自动填表单与登录功能的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

Python参数注解的另类用法在Python编程中,参数注解是一种非常有用的功能,可以帮助开发者更好地理解和使用函...

Python跨平台桌面应用开发库的选择许多Python开发者都希望开发出能够在Windows和Linux系统上都能运行的桌面应用程...

为什么我的代码无法获取API返回的数据?在编程中,我们常常会遇到API调用时返回空值的问题,这不仅让人困惑...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

Python脚本如何在特定位置清空输出到光标位置?在编写Python脚本时,如何清空之前的输出到光标位置是个常见的...
