在视频上传之后出现需要点击图片中的 文字 验证,怎么用python 解决这个验证的问题,实现自动验证
【相关推荐】
首先,我们需要导入需要的资源库包。
结合GPT给出回答如下请题主参考
要实现网站的文字验证自动化,需要使用Python模拟用户的行为,即通过代码自动对网页进行访问和交互。下面是一个基本的步骤:
from selenium import webdriver
driver = webdriver.Chrome() # 使用Chrome驱动
driver.get("https://example.com/validation-page") # 进入需要验证的页面
from PIL import Image
import os
# 定位验证码图片元素
captcha_element = driver.find_element_by_xpath("//img[@id='captcha-image']")
# 获取验证码图片的base64编码
captcha_base64 = driver.execute_script("return arguments[0].attributes['src'].value;", captcha_element)
# 将base64编码转换为图片,并保存到本地
with open("captcha.png", "wb") as f:
f.write(base64.b64decode(captcha_base64.split(",")[1]))
# 用PIL打开图片
captcha_img = Image.open(os.path.join(os.getcwd(), "captcha.png"))
import pytesseract
captcha_text = pytesseract.image_to_string(captcha_img)
captcha_input = driver.find_element_by_xpath("//input[@id='captcha-input']")
captcha_input.send_keys(captcha_text)
submit_button = driver.find_element_by_xpath("//button[@id='submit-button']")
submit_button.click()
if "验证成功" in driver.page_source:
print("验证成功!")
# 进行后续的操作
else:
print("验证失败!")
总的来说,实现网站文字验证自动化需要使用Selenium和PIL等模块,同时需要进行验证码识别和输入等操作。需要注意的是,一些网站可能会对自动化进行限制或封禁,因此需要谨慎使用。
可以借助图像处理库和自动化库来实现,使用Python的Pillow库进行图像处理,以及使用pyautogui库进行自动化操作:
pip install Pillow pyautogui
captcha.png
:from PIL import Image
import pytesseract
import pyautogui
def solve_captcha(image_path):
# 打开图像
image = Image.open(image_path)
# 图像处理(根据具体情况进行调整)
# 例如,可以使用图像剪裁、缩放、二值化等操作来提取文字部分
# 提取文字
captcha_text = pytesseract.image_to_string(image)
# 自动点击验证文字位置(根据具体情况进行调整)
text_position = pyautogui.locateCenterOnScreen('captcha_text.png')
pyautogui.click(text_position)
# 输入验证码
pyautogui.typewrite(captcha_text)
# 调用函数解决验证码问题
solve_captcha('captcha.png')
python来实现自动化程序识别验证文字
py
# coding:utf-8
import datetime
import math
import os
import random
import re
import sys
import time
from io import BytesIO
import ddddocr
from PIL import Image, ImageDraw
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from api import get_tiiktok_anti2
import execjs
import requests
from colorama import Fore, init
import api
import cooke_path_8
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
# 获取当前文件的目录
cur_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(cur_path)
cookie_path = cooke_path_8.get_cooke_path_8()
current_path = cooke_path_8.get_current_path()
get_chromedriver_path = cooke_path_8.get_chromedriver_path()
import logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
'''
nid =1
ss1= "https://mobile.pinduoduo.com/goods.html?goods_id={0}&force_use_web_bundle=1".format(str(nid))
ss2= "https://mobile.pinduoduo.com/goods.html?goods_id={}&force_use_web_bundle=1".format(str(nid))
print(ss1)
print(ss2)
sys.exit(1)
'''
class UpData:
def __init__(self, page_max, which):
#m3 = execjs.compile(open(r"route.js", encoding='utf-8').read())
#verifyFp = m3.call('mergeFp', '')
#print(verifyFp)
#exit(1)
#dXNlcg==
#anNzd3h2Mmc==
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-blink-features=AutomationControlled") # 禁用启用Blink运行时的功能
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) # 去除浏览器检测框
#chrome_options.add_argument('--user-data-dir=' + cookie_path)
#chrome_options.add_argument('--proxy-server=http://127.0.0.1:8999')
self.driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path=get_chromedriver_path)
'''
with open(f'{cur_path}' + '/stealth.min.js', encoding='utf-8') as f:
js = f.read()
self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})
'''
self.page_max = page_max + 1
self.page_no = 1
self.page_size = 200
self.detail_sleep_time = 1
self.debug = 0
self.begin_date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y/%m/%d')
self.end_date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y/%m/%d')
self.headers = {
'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
}
self.getVerificationCode()
def getVerificationCode(self):
base_url = f"https://www.kgcaptcha.com/demo/content?t=4&cindex=2#code_lang"
base_url = f"http://127.0.0.1/test.html"
self.driver.get(base_url)
time.sleep(5)
wait = WebDriverWait(self.driver, 10)
image2 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="verify-bar-code"]')))
target_img_url = image2.get_attribute('src')
target_image_content = requests.get(target_img_url).content
docr = ddddocr.DdddOcr(show_ad=False)
target_words = docr.classification(target_image_content)
image1 = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="captcha-verify-image"]')))
background_img_url = image1.get_attribute('src')
background_image_content = requests.get(background_img_url).content
ddet = ddddocr.DdddOcr(det=True, show_ad=False)
poses = ddet.detection(background_image_content)
img = Image.open(BytesIO(background_image_content))
draw = ImageDraw.Draw(img)
click_identify_result = {}
for row in poses:
# 框字
row = (row[0] - 3, row[1] - 3, row[2] + 3, row[3] + 3)
x1, y1, x2, y2 = row
draw.line(([(x1, y1), (x1, y2), (x2, y2), (x2, y1), (x1, y1)]), width=1, fill='red')
# 裁剪出单个字
corp = img.crop(row)
img_byte = BytesIO()
corp.save(img_byte, 'png')
# 识别出单个字
word = docr.classification(img_byte.getvalue())
click_identify_result[word] = row
img.show()
# 计算文字点击坐标
img_xy = {}
for key, xy in click_identify_result.items():
if key:
img_xy[key] = (int((xy[0] + xy[2]) / 2), int((xy[1] + xy[3]) / 2))
logger.info(img_xy)
# 计算最终点击顺序与坐标
result = {}
for word in target_words:
result[word] = img_xy[word]
logger.info(result)
# 点击坐标
image1_x = image1.location.get('x')
image1_y = image1.location.get('y')
for xy in result.values():
x = xy[0] * (340 / 552)
y = xy[1] * (212 / 344)
ActionChains(self.driver).reset_actions()
ActionChains(self.driver).move_by_offset(image1_x + x, image1_y + y).click().perform()
time.sleep(6000)
if __name__ == '__main__':
updata = UpData(1, 2)
from selenium import webdriver
# 创建一个Chrome浏览器实例
driver = webdriver.Chrome()
# 打开登录页面
driver.get("https://example.com/login")
# 输入用户名和密码
driver.find_element_by_name("username").send_keys("your_username")
driver.find_element_by_name("password").send_keys("your_password")
# 点击登录按钮
driver.find_element_by_css_selector("button[type='submit']").click()
# 点击验证码图片
driver.find_element_by_css_selector("img[alt='captcha']").click()
# 输入验证码
driver.find_element_by_name("captcha").send_keys("your_captcha")
# 点击提交按钮
driver.find_element_by_css_selector("button[type='submit']").click()
# 关闭浏览器实例
driver.quit()
img = Image.open('image.png')
text = pytesseract.image_to_string(img)
打开一个Chrome浏览器实例,访问你的网站页面,执行上传视频的操作,然后找到文字验证的元素,并从中获取验证码图片的alt属性(即文字),然后将文字输入到验证码输入框中,最后点击提交按钮。最后记得关闭浏览器实例
参考gpt
要实现网站文字验证的自动化,您可以使用Python和Selenium库来模拟用户操作,自动点击图片中的文字验证。
以下是一个示例代码,展示了如何使用Selenium来处理网站的文字验证:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
# 创建一个浏览器实例
driver = webdriver.Chrome()
# 打开网页
driver.get("https://example.com")
# 定位到需要点击的图片元素
image_element = driver.find_element_by_xpath("//img[@class='captcha-image']")
# 获取图片的位置和大小
image_location = image_element.location
image_size = image_element.size
# 计算图片的左上角和右下角坐标
image_left = image_location['x']
image_top = image_location['y']
image_right = image_left + image_size['width']
image_bottom = image_top + image_size['height']
# 创建一个ActionChains对象
actions = ActionChains(driver)
# 在图片上执行鼠标点击操作
actions.move_to_element_with_offset(image_element, image_left + 10, image_top + 10)
actions.click()
actions.perform()
# 输入验证码文字
verification_text = input("请输入验证码文字:")
verification_input = driver.find_element_by_xpath("//input[@class='verification-input']")
verification_input.send_keys(verification_text)
# 提交表单
submit_button = driver.find_element_by_xpath("//button[@class='submit-button']")
submit_button.click()
# 关闭浏览器
driver.quit()
请根据实际情况修改代码中的网页URL、图片元素的定位方式和验证码输入框的定位方式。您可能需要使用其他的定位方式,例如根据元素的ID、class、name等进行定位。
此外,您还需要安装Selenium库和浏览器驱动程序(如Chrome驱动),并将驱动程序的路径配置到系统环境变量中。
这个要使用OCR来识别
要进行图片识别,比较要难度的,可以用现成的库来做
一些网站可能会采取反爬虫措施,例如使用图像验证码或其他复杂的验证方式,对于这种情况,您可能需要进一步研究和尝试其他解决方案,如OCR技术或机器学习型