使用 selenium 处理计算验证码实现168网课登录效果
~~~ pythonfrom selenium import webdriver
import time
from selenium.webdriver.common.by import By
from PIL import Image
import ddddocr
import time
driver = webdriver.Chrome()
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
# 忽略证书错误
option.add_argument('--ignore-certificate-errors')
# 忽略 Bluetooth: bluetooth_adapter_winrt.cc:1075 Getting Default Adapter failed. 错误
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 忽略 DevTools listening on ws://127.0.0.1... 提示
option.add_experimental_option('excludeSwitches', ['enable-logging'])
# 获取driver对象, 并将配置好的option传入进去 运行结束不关闭窗口
driver = webdriver.Chrome(options=option)
driver.get('https://xatu.168wangxiao.com/web/login?redirect=%2Fhome')
driver.maximize_window()
time.sleep(3)
driver.find_element(By.XPATH,"/html/body/div/div/div/div/div/div/div/div/div/form/div/div/div/input").send_keys("username")
driver.find_element(By.XPATH,"/html/body/div/div/div/div/div/div/div/div/div/form/div/div/div/input").send_keys("password")
# 防止图片识别错误 try except 处理
while True:
try:
svgHeml=driver.find_element(By.XPATH,'/html/body/div/div/div/div/div/div/div/div/div/form/div/div/div/div')
# 获取元素的位置和尺寸
location = svgHeml.location
size = svgHeml.size
# 计算四个角的坐标
x1 = location['x']
y1 = location['y']
x2 = location['x'] + size['width']
y2 = location['y'] + size['height']
# 输出四个角的坐标
print("左上角坐标: (x1, y1) = ({0}, {1})".format(x1, y1))
print("右上角坐标: (x2, y1) = ({0}, {1})".format(x2, y1))
print("左下角坐标: (x1, y2) = ({0}, {1})".format(x1, y2))
print("右下角坐标: (x2, y2) = ({0}, {1})".format(x2, y2))
# 截全图
screenshot_path = 'screenshot.png'
driver.save_screenshot(screenshot_path)
# # time.sleep(3)
# # # 截取指定区域
im = Image.open(screenshot_path)
region = im.crop((x1, y1, x2, y2))
# # 保存截图
cropped_path = 'result_screenshot.png'
region.save(cropped_path)
start = time.time() # 开始时间
# 1. 创建DdddOcr对象
ocr = ddddocr.DdddOcr(show_ad=False)
# 2. 读取图片
with open(cropped_path, 'rb') as f:
img = f.read()
# 3. 识别图片内验证码并返回字符串
result = ocr.classification(img)
print("识别结果:",result)
num = eval(result)
# eval()函数的安全性问题,因为它会执行字符串中的任何Python代码,可能会导致安全漏洞,所以仅在可信的字符串上使用它
# 方法二
# import ast
# expression = "3 + 4 * 2"
# result = ast.literal_eval(expression)
# print(result)
print(result)
print(num)
end = time.time()
print("耗时:%s 秒" % str(start-end))
driver.find_element(By.XPATH,"/html/body/div/div/div/div/div/div/div/div/div/form/div/div/div/input").send_keys(5)
driver.find_element(By.XPATH,"/html/body/div/div/div/div/div/div/div/div/div/form/div/div/button").click()
except:
# 以防万一 图片识别错误 重新点击图片 重新获取验证码 执行try
driver.find_element(By.XPATH,'/html/body/div/div/div/div/div/div/div/div/div/form/div/div/div/div').click()
time.sleep(3)
~~~ `while True:` 加上 `try/except` 死循环了哦。
改写方案:
```
while True:
try:
pass # 处理代码
except:
pass # 遇到错误时
else:
break # 没有错误时,跳出 while True 的循环
``` ocr识别? 78zhanghao87 发表于 2024-4-12 08:00
ocr识别?
ddddocr(带带弟弟 OCR ) 太强了,给楼主点个赞赞啊 本帖最后由 Pwaerm 于 2024-4-12 08:54 编辑
168哇哇叫
168平台很多bug
里面源码一看就是菜鸟写的
不用登陆都可以 可以直接灌入数据 感谢分享 新技术,感谢分享!学习💪 太厉害了!给你点赞 ocr识别率怎么样?
页:
[1]
2