这里获取的是背景以及滑块图片
- 获取图片
通过requests.get(),将图片下载到本地
with open('./yuan/image.html','r',encoding='utf-8') as fp:
page_text = fp.read()
html = etree.HTML(page_text)
img1 = html.xpath('/html/head/link[4]/@href')
img2 = html.xpath('/html/head/link[5]/@href')
imgCon1 = requests.get(url=img1[0],headers=headers).content
imgCon2 = requests.get(url=img2[0],headers=headers).content
List = []
with open('./img/background.png','wb') as fp:
fp.write(imgCon1)
with open('./img/frag.png', 'wb') as fp:
fp.write(imgCon2)
def _tran_canny(image):
"""降噪"""
image = cv2.GaussianBlur(image, (3, 3), 0)
return cv2.Canny(image, 50, 150)
def detect_displacement(img_slider_path, image_background_path):
"""detect displacement"""
# 灰度化
image = cv2.imread(img_slider_path, 0)
template = cv2.imread(image_background_path, 0)
# 图片模板位置匹配 归一化相关系数匹配法cv2.TM_CCOEFF_NORMED
res = cv2.matchTemplate(_tran_canny(image), _tran_canny(template), method=cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc[0] # 横坐标
print(top_left)
return top_left
def get_tracks(distance):
# 构造滑动轨迹
tracks = []
v = 0
t = 0.9 # 单位时间
current = 0 # 滑块当前位移
distance += 10 # 多移动10px,然后回退
while current < distance:
if current < distance * 5 / 8:
a = random.randint(1, 3)
else:
a = -random.randint(2, 4)
v0 = v # 初速度
track = v0 * t + 0.5 * a * (t ** 2) # 单位时间(0.2s)的滑动距离
tracks.append(round(track)) # 加入轨迹
current += round(track)
v = v0 + a * t
# 回退到大致位置
for i in range(5):
tracks.append(-random.randint(1, 2))
# tracks.append(-random.choice([0.3, 0.5, 0.8]))
return tracks
# from selenium.webdriver.support.wait import WebDriverWait
# webDriver = Chrome(executable_path=path,options=chrome_options)
# wait1 = WebDriverWait(webDriver, 3)
# browser = webDriver
# trace 就是图片处理返回的距离
def move_to_gap(wait1,browser,trace):
# 得到滑块标签
# slider = wait1.until(EC.presence_of_element_located((By.CLASS_NAME, 'verify-move-block')))
slider = wait1.until(EC.presence_of_element_located((By.XPATH, '//*[@id="captcha__frame__bottom"]/div[2]/div[2]')))
# 使用click_and_hold()方法悬停在滑块上,perform()方法用于执行
ActionChains(browser).click_and_hold(slider).perform()
for x in trace:
# 使用move_by_offset()方法拖动滑块,perform()方法用于执行
ActionChains(browser).move_by_offset(xoffset=x, yoffset=0).perform()
# 模拟人类对准时间
sleep(0.5)
# 释放滑块
ActionChains(browser).release().perform()
# ** 幂 - 返回x的y次幂 a**b 为10的20次方, 输出结果 100000000000000000000
# round() 方法返回浮点数x的四舍五入值。
# random.randint() 方法返回指定范围内的整数。
randint(start, stop) 等价于 randrange(start, stop+1)。
# choice() 方法返回一个列表,元组或字符串的随机项。
如果用selenium,在控制台输入window.navigator.webdriver 会显示true
# 1.chrome的版本号如果小于88
在启动浏览器的时候(此时没有加载任何内容时),将页面嵌入js代码,去掉webdriver
2.chrome的版本号如果大于88
from selenium.webdriver.chrome.options import Options
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
本次参考模仿的案例