from PIL import Image
def handler(grays, threshold=160):
"""
对灰度图片进行二值化处理,默认阈值为160,可根据实际情况调整
"""
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
anti = grays.point(table, "1")
return anti
# 灰度处理
images = "code.png"
gray = Image.open(images).convert("L")
gray.show()
# 二值化处理
image = handler(gray)
image.show()
深度学习基本了解
import pytesseract
import re
def operator_func(a:int, b:int, oper:str) -> int:
# 接收两个值和运算符,返回数字运算结果
if oper == "+":
return a+b
if oper == "-":
return a-b
if oper == "*":
return a*b
strings = pytesseract.image_to_string("paste.png")
result = re.findall(r"\d+", strings)
operator = re.findall(r'[\+|\-|\*]', result)
res = operator_func(int(result[0]), int(result[1]), operator[0])
print(res)
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.porters.vip/captcha/sliders.html")
hover = browser.find_element_by_css_selector(".hover") # 定位滑块
action = webdriver.ActionChains(browser)
action.click_and_hold(hover).perform() # 点击并保持不松开
action.move_by_offset(340, 0) # 设置滑动距离,横向距离为340px(390-50),纵向距离为0px
action.release().perform() # 松开鼠标
from selenium import webdriver
import re
from parsel import Selector
browser = webdriver.Chrome()
browser.get("http://www.porters.vip/captcha/jigsaw.html")
jigsaw = browser.find_element_by_css_selector("#jigsawCircle") # 定位滑块
action = webdriver.ActionChains(browser)
action.click_and_hold(jigsaw).perform() # 点击并保持不松开
html = browser.page_source # 返回当前页面的html代码
sel = Selector(html)
# 获取圆角矩形和缺口的css样式
mbk_style = sel.css("#missblock::attr('style')").get()
tbk_style = sel.css("#targetblock::attr('style')").get()
# 调用匿名函数获取CSS样式中的left属性值
mbk_left = re.search(r"left: (\d+\.?\d+)px", mbk_style).group(1)
tbk_left = re.search(r"left: (\d+\.?\d+)px", tbk_style).group(1)
# 计算当前拼图验证码滑块所需的移动距离
distance = float(tbk_left) - float(mbk_left)
action.move_by_offset(distance, 0) # 设置滑动距离,横向距离为340px(390-50),纵向距离为0px
action.release().perform() # 松开鼠标
from selenium import webdriver
from PIL import ImageChops
from PIL import Image
browser = webdriver.Chrome()
browser.get("http://www.porters.vip/captcha/jigsawCanvas.html")
jigsaw = browser.find_element_by_css_selector("#jigsawCircle") # 定位滑块
canvas = browser.find_element_by_css_selector("#jigsawCanvas")
canvas.screenshot("before.png")
action = webdriver.ActionChains(browser)
action.click_and_hold(jigsaw).perform() # 点击并保持不松开
# 执行javascript隐藏圆角矩形的HTML代码
scripts = """
var missblock = document.getElementById("missblock");
missblock.style['visibility'] = 'hidden';
"""
browser.execute_script(scripts)
# 再次截图
canvas.screenshot("after.png")
image_a = Image.open("after.png")
image_b = Image.open("before.png")
# 使用ImageChops模块中的difference()方法对比图片像素的不同,并获取图片差异位置的坐标
diff = ImageChops.difference(image_b, image_a)
# 获取图片差异位置的坐标
diff_position = diff.getbbox() # (左、上、右、下)
print(diff_position)
position_x = diff_position[0]
action.move_by_offset(int(position_x)-10, 0)
action.release().perform() # 松开鼠标
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.porters.vip/captcha/mousemove.html")
button = browser.find_element_by_class_name("button1")
button.click()
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.porters.vip/captcha/mousemove.html")
button = browser.find_element_by_class_name("button1")
action = webdriver.ActionChains(browser)
action.click_and_hold(button).perform() # 点击并保持不松开
action.move_by_offset(340, 5) # 设置滑动距离,横向距离和纵向距离
action.release().perform() # 松开鼠标