selenium
pip install selenium
Selenium想要控制谷歌、火狐、IE、Edage等浏览器,必须要使用对应的驱动才行。【Selenium】->【驱动】->【浏览器】
【Selenium】->【火狐驱动】->【火狐浏览器】
【Selenium】->【谷歌驱动】->【谷歌浏览器】
谷歌驱动的下载:
114及之前版本: http://chromedriver.storage.googleapis.com/index.html
117/118/119版本: https://googlechromelabs.github.io/chrome-for-testing/
浏览器版本的获取:
在谷歌浏览器上访问 chrome://version/ 例如:119.0.6045.200 (正式版本) (64 位) (cohort: Stable)
我之前把谷歌的升级给禁了,找的是118的版本
https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/118.0.5993.18/win64/chromedriver-win64.zip
识别图片
获取图片
import re
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
service = Service("driver/chromedriver.exe")
driver = webdriver.Chrome(service=service)
# 1.打开首页
driver.get('https://www.geetest.com/adaptive-captcha-demo')
# 2.点击【滑动拼图验证】
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
By.XPATH,
'//*[@id="gt-showZh-mobile"]/div/section/div/div[2]/div[1]/div[2]/div[3]/div[3]'
))
tag.click()
# 3.点击开始验证
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.fi服务器托管网nd_element(
By.CLASS_NAME,
'geetest_btn_click'
))
tag.click()
# 4.读取背景图片
def fetch_bg_func(dv):
tag_object = dv.find_element(
By.CLASS_NAME,
'geetest_bg'
)
style_string = tag_object.get_attribute("style")
match_list = re.findall('url("(.*)");', style_string) # ["http..." ] []
if match_list:
return match_list[0]
bg_image_url = WebDriverWait(dr服务器托管网iver, 30, 0.5).until(fetch_bg_func) # 新的函数 = 某个函数('geetest_bg')
print("背景图:", bg_image_url)
# 4.读取缺口图片
def fetch_slice_func(dv):
tag_object = dv.find_element(
By.CLASS_NAME,
'geetest_slice_bg'
)
style_string = tag_object.get_attribute("style")
match_list = re.findall('url("(.*)");', style_string)
if match_list:
return match_list[0]
slice_image_url = WebDriverWait(driver, 30, 0.5).until(fetch_slice_func) # 新的函数 = 某个函数('geetest_slice_bg')
print("缺口图:", slice_image_url)
time.sleep(2000)
driver.close()
这里用ddddocr
import ddddocr
import requests
slice_bytes = requests.get("缺口图片地址").content
bg_bytes = requests.get("背景图片地址").content
slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
res = slide.slide_match(slice_bytes, bg_bytes, simple_target=True)
x1, y1, x2, y2 = res['target']
print(x1, y1, x2, y2) # 114 45 194 125
opencv
import cv2
import numpy as np
import requests
def get_distance(bg_bytes, slice_bytes):
def get_image_object(byte_image):
img_buffer_np = np.frombuffer(byte_image, dtype=np.uint8)
img_np = cv2.imdecode(img_buffer_np, 1)
bg_img = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
return bg_img
bg_image_object = get_image_object(bg_bytes)
slice_image_object = get_image_object(slice_bytes)
# 边缘检测
bg_edge = cv2.Canny(bg_image_object, 255, 255)
tp_edge = cv2.Canny(slice_image_object, 255, 255)
bg_pic = cv2.cvtColor(bg_edge, cv2.COLOR_GRAY2RGB)
tp_pic = cv2.cvtColor(tp_edge, cv2.COLOR_GRAY2RGB)
res = cv2.matchTemplate(bg_pic, tp_pic, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 寻找最优匹配
x = max_loc[0]
return x
slice_bytes = requests.get("缺口图片地址").content
bg_bytes = requests.get("背景图片地址").content
distance = get_distance(bg_bytes, slice_bytes)
print(distance)
Selenium滑动
from selenium.webdriver import ActionChains
tag = driver.find_element(By.CLASS_NAME, 'geetest_btn')
ActionChains(driver).click_and_hold(tag).perform() # 点击并抓住标签
ActionChains(driver).move_by_offset(xoffset=114, yoffset=0).perform() # 向右滑动114像素(向左是负数)
ActionChains(driver).release().perform() # 释放
中文点选
import time
import ddddocr
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
service = Service("driver/chromedriver.exe")
driver = webdriver.Chrome(service=service)
# 1.打开首页
driver.get('https://www.geetest.com/adaptive-captcha-demo')
# 2.点击【滑动拼图验证】
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
By.XPATH,
'//*[@id="gt-showZh-mobile"]/div/section/div/div[2]/div[1]/div[2]/div[3]/div[4]'
))
tag.click()
# 3.点击开始验证
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
By.CLASS_NAME,
'geetest_btn_click'
))
tag.click()
# 4.等待验证码出来
time.sleep(5)
# 5.识别任务图片
target_word_list = []
parent = driver.find_element(By.CLASS_NAME, 'geetest_ques_back')
tag_list = parent.find_elements(By.TAG_NAME, "img")
for tag in tag_list:
ocr = ddddocr.DdddOcr(show_ad=False)
word = ocr.classification(tag.screenshot_as_png)
target_word_list.append(word)
print("要识别的文字:", target_word_list)
time.sleep(2000)
driver.close()
如果觉得识别率低,可以选择氪金
https://www.chaojiying.com/
求取坐标
import re
import time
import ddddocr
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
from PIL import Image, ImageDraw
from io import BytesIO
service = Service("driver/chromedriver.exe")
driver = webdriver.Chrome(service=service)
# 1.打开首页
driver.get('https://www.geetest.com/adaptive-captcha-demo')
# 2.点击【滑动拼图验证】
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
By.XPATH,
'//*[@id="gt-showZh-mobile"]/div/section/div/div[2]/div[1]/div[2]/div[3]/div[4]'
))
tag.click()
# 3.点击开始验证
tag = WebDriverWait(driver, 30, 0.5).until(lambda dv: dv.find_element(
By.CLASS_NAME,
'geetest_btn_click'
))
tag.click()
# 4.等待验证码出来
time.sleep(5)
# 5.识别任务图片
target_word_list = []
parent = driver.find_element(By.CLASS_NAME, 'geetest_ques_back')
tag_list = parent.find_elements(By.TAG_NAME, "img")
for tag in tag_list:
ocr = ddddocr.DdddOcr(show_ad=False)
word = ocr.classification(tag.screenshot_as_png)
target_word_list.append(word)
print("要识别的文字:", target_word_list)
# 6.背景图片
bg_tag = driver.find_element(
By.CLASS_NAME,
'geetest_bg'
)
content = bg_tag.screenshot_as_png
# 7.识别背景中的所有文字并获取坐标
ocr = ddddocr.DdddOcr(show_ad=False, det=True)
poses = ocr.detection(content) # [(x1, y1, x2, y2), (x1, y1, x2, y2), x1, y1, x2, y2]
# 8.循环坐标中的每个文字并识别
bg_word_dict = {}
img = Image.open(BytesIO(content))
for box in poses:
x1, y1, x2, y2 = box
# 根据坐标获取每个文字的图片
corp = img.crop(box)
img_byte = BytesIO()
corp.save(img_byte, 'png')
# 识别文字
ocr2 = ddddocr.DdddOcr(show_ad=False)
word = ocr2.classification(img_byte.getvalue()) # 识别率低
# 获取每个字的坐标 {"鸭":}
bg_word_dict[word] = [int((x1 + x2) / 2), int((y1 + y2) / 2)]
print(bg_word_dict)
time.sleep(1000)
driver.close()
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 蜘点云原生之 KubeSphere 落地实践过程
【直播预告】上云 vs 下云:降本增笑?割韭菜? 作者:池晓东,蜘点商业网络服务有限公司技术总监,从事软件开发设计 10 多年,喜欢研究各类新技术,分享技术。 来源:本文由 11 月 25 日广州站 meetup 中讲师池晓东整理,整理于该活动中池老师所分享的…