版權聲明:希望與廣大Python愛好者,可以相互交流,歡迎留言!歡迎轉載(請注明出處) https://blog.csdn.net/EB_NUM/article/details/78394958
我們首先理解滑動驗證的原理
滑動驗證難點
1.電腦如何自動點擊滑動塊
2.電腦如何檢測 缺口位置(如圖;)
解決這兩個問題方法
- 如何自動點擊滑動塊,也就是圖中的左下方圈起來的位置,我們可以使用selenium
- 怎么計算缺口的位置,我們可以通過PIL庫的image
既然有了解決方法,我們看一下源碼,源碼中,我已經添加了注釋
# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import PIL.Image as image
from PIL import Image,ImageEnhance
import time,re, random
import requests
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
#爬蟲模擬的瀏覽器頭部信息
agent = "Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0"
headers = {
"User-Agent": agent
}
# 根據位置對圖片進行合並還原
# filename:圖片
# location_list:圖片位置
#內部兩個圖片處理函數的介紹
#crop函數帶的參數為(起始點的橫坐標,起始點的縱坐標,寬度,高度)
#paste函數的參數為(需要修改的圖片,粘貼的起始點的橫坐標,粘貼的起始點的縱坐標)
def get_merge_image(filename,location_list):
#打開圖片文件
im = image.open(filename)
#創建新的圖片,大小為260*116
new_im = image.new("RGB", (260,116))
im_list_upper=[]
im_list_down=[]
# 拷貝圖片
for location in location_list:
#上面的圖片
if location["y"]==-58:
im_list_upper.append(im.crop((abs(location["x"]),58,abs(location["x"])+10,166)))
#下面的圖片
if location["y"]==0:
im_list_down.append(im.crop((abs(location["x"]),0,abs(location["x"])+10,58)))
new_im = image.new("RGB", (260,116))
x_offset = 0
#黏貼圖片
for im in im_list_upper:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
x_offset = 0
for im in im_list_down:
new_im.paste(im, (x_offset,58))
x_offset += im.size[0]
return new_im
#對比RGB值
def is_similar(image1,image2,x,y):
pass
#獲取指定位置的RGB值
pixel1=image1.getpixel((x,y))
pixel2=image2.getpixel((x,y))
for i in range(0,3):
# 如果相差超過50則就認為找到了缺口的位置
if abs(pixel1[i]-pixel2[i])>=50:
return False
return True
#計算缺口的位置
def get_diff_location(image1,image2):
i=0
# 兩張原始圖的大小都是相同的260*116
# 那就通過兩個for循環依次對比每個像素點的RGB值
# 如果相差超過50則就認為找到了缺口的位置
for i in range(62,260):#有人可能看不懂這個位置為什么要從62開始看最后一張圖(圖:3)
for j in range(0,116):
if is_similar(image1,image2,i,j)==False:
return i
#根據缺口的位置模擬x軸移動的軌跡
def get_track(length):
pass
list=[]
#間隔通過隨機范圍函數來獲得,每次移動一步或者兩步
x=random.randint(1,3)
#生成軌跡並保存到list內
while length-x>=5:
list.append(x)
length=length-x
x=random.randint(1,3)
#最后五步都是一步步移動
for i in range(length):
list.append(1)
return list
#滑動驗證碼破解程序
def main():
#打開火狐瀏覽器
driver = webdriver.Firefox()
#用火狐瀏覽器打開網頁
driver.get("https://account.geetest.com/register")
time.sleep(2)
driver.find_element_by_xpath('//*[@id="captcha"]/div/div[3]/span[2]').click()
time.sleep(5)
driver.get_screenshot_as_file("D:/test2/滑動驗證/img.jpg")#對整個頁面截圖
imgelement = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[2]/div[1]/div/a/div[1]/canvas') # 定位驗證碼
location = imgelement.location # 獲取驗證碼x,y軸坐標
size = imgelement.size # 獲取驗證碼的長寬
rangle = (int(location['x'] ), int(location['y']), int(location['x'] + size['width']),
int(location['y'] + size['height'])) # 寫成我們需要截取的位置坐標
i = Image.open("D:/test2/滑動驗證/img.jpg") # 打開截圖
i = i.convert('RGB')
frame1 = i.crop(rangle) # 使用Image的crop函數,從截圖中再次截取我們需要的區域
frame1.save('D:/test2/滑動驗證/new.jpg')
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[2]/div[2]/div[2]').click()
time.sleep(4)
driver.get_screenshot_as_file("D:/test2/滑動驗證/img.jpg")
imgelement = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[2]/div[1]/div/a/div[1]/div/canvas[2]') # 定位驗證碼
location = imgelement.location # 獲取驗證碼x,y軸坐標
size = imgelement.size # 獲取驗證碼的長寬
rangle = (int(location['x'] ), int(location['y']), int(location['x'] + size['width']),
int(location['y'] + size['height'])) # 寫成我們需要截取的位置坐標
i = Image.open("D:/test2/滑動驗證/img.jpg") # 打開截圖
i = i.convert('RGB')
frame2 = i.crop(rangle) # 使用Image的crop函數,從截圖中再次截取我們需要的區域
frame2.save('D:/test2/滑動驗證/new2.jpg')
#計算缺口位置
loc=get_diff_location(frame1, frame2)
print('-------------')
print(loc)
#找到滑動的圓球
element=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div/div[2]/div[2]/div[2]')
location=element.location
#獲得滑動圓球的高度
y=location["y"]
#鼠標點擊元素並按住不放
print ("第一步,點擊元素")
ActionChains(driver).click_and_hold(on_element=element).perform()
time.sleep(0.15)
print ("第二步,拖動元素")
ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=loc + 30, yoffset=y - 445).perform()
#釋放鼠標
ActionChains(driver).release(on_element=element).perform()
#關閉瀏覽器,為了演示方便,暫時注釋掉.
#driver.quit()
#主函數入口
if __name__ == "__main__":
pass
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153