python滑动验证码处理_python+selenium滑动式验证码解决办法
from selenium importwebdriverfrom selenium.webdriver.support.ui import WebDriverWait #等待元素加載的
from selenium.webdriver.common.action_chains import ActionChains #拖拽
from selenium.webdriver.support importexpected_conditions as ECfrom selenium.common.exceptions importTimeoutException, NoSuchElementExceptionfrom selenium.webdriver.common.by importByfrom PIL importImageimportrequestsimportreimportrandomfrom io importBytesIOimporttimedefmerge_image(image_file,location_list):"""拼接圖片"""im=Image.open(image_file)
im.save('code.jpg')
new_im= Image.new('RGB',(260,116))#把無(wú)序的圖片 切成52張小圖片
im_list_upper =[]
im_list_down=[]#print(location_list)
for location inlocation_list:#print(location['y'])
if location['y'] == -58: #上半邊
im_list_upper.append(im.crop((abs(location['x']),58,abs(location['x'])+10,116)))if location['y'] == 0: #下半邊
im_list_down.append(im.crop((abs(location['x']),0,abs(location['x'])+10,58)))
x_offset=0for im inim_list_upper:
new_im.paste(im,(x_offset,0))#把小圖片放到 新的空白圖片上
x_offset +=im.size[0]
x_offset=0for im inim_list_down:
new_im.paste(im,(x_offset,58))
x_offset+=im.size[0]#new_im.show()
returnnew_imdefget_image(driver,div_path):'''下載無(wú)序的圖片 然后進(jìn)行拼接 獲得完整的圖片
:param driver:
:param div_path:
:return:'''background_images=driver.find_elements_by_xpath(div_path)
location_list=[]for background_image inbackground_images:
location={}
result= re.findall('background-image: url\("(.*?)"\); background-position: (.*?)px (.*?)px;',background_image.get_attribute('style'))#print(result)
location['x'] = int(result[0][1])
location['y'] = int(result[0][2])
image_url=result[0][0]
location_list.append(location)
image_url= image_url.replace('webp','jpg')#'替換url http://static.geetest.com/pictures/gt/579066de6/579066de6.webp'
image_result =requests.get(image_url).content
image_file= BytesIO(image_result) #是一張無(wú)序的圖片
image =merge_image(image_file,location_list)returnimagedefget_track(distance):#初速度
v=0#單位時(shí)間為0.2s來(lái)統(tǒng)計(jì)軌跡,軌跡即0.2內(nèi)的位移
t=0.2
#位移/軌跡列表,列表內(nèi)的一個(gè)元素代表0.2s的位移
tracks=[]
tracks_back=[]#當(dāng)前的位移
current=0#到達(dá)mid值開(kāi)始減速
mid=distance * 7/8
print("distance",distance)globalrandom_int
random_int=8distance+= random_int #先滑過(guò)一點(diǎn),最后再反著滑動(dòng)回來(lái)
while current
a = random.randint(2,5) #加速運(yùn)動(dòng)
else:
a= -random.randint(2,5) #減速運(yùn)動(dòng)
#初速度
v0 =v#0.2秒時(shí)間內(nèi)的位移
s = v0*t+0.5*a*(t**2)#當(dāng)前的位置
current +=s#添加到軌跡列表
if round(s)>0:
tracks.append(round(s))else:
tracks_back.append(round(s))#速度已經(jīng)達(dá)到v,該速度作為下次的初速度
v= v0+a*tprint("tracks:",tracks)print("tracks_back:",tracks_back)print("current:",current)#反著滑動(dòng)到大概準(zhǔn)確位置
tracks_back.append(distance-current)
tracks_back.extend([-2,-5,-8,])returntracks,tracks_backdefget_distance(image1,image2):'''拿到滑動(dòng)驗(yàn)證碼需要移動(dòng)的距離
:param image1:沒(méi)有缺口的圖片對(duì)象
:param image2:帶缺口的圖片對(duì)象
:return:需要移動(dòng)的距離'''
#print('size', image1.size)
threshold= 50
for i in range(0,image1.size[0]): #260
for j in range(0,image1.size[1]): #160
pixel1 =image1.getpixel((i,j))
pixel2=image2.getpixel((i,j))
res_R= abs(pixel1[0]-pixel2[0]) #計(jì)算RGB差
res_G = abs(pixel1[1] - pixel2[1]) #計(jì)算RGB差
res_B = abs(pixel1[2] - pixel2[2]) #計(jì)算RGB差
if res_R > threshold and res_G > threshold and res_B >threshold:return i #需要移動(dòng)的距離
defmain_check_code(driver,element):"""拖動(dòng)識(shí)別驗(yàn)證碼
:param driver:
:param element:
:return:"""login_btn= driver.find_element_by_class_name('js-login')
login_btn.click()
element= WebDriverWait(driver, 30, 0.5).until(EC.element_to_be_clickable((By.CLASS_NAME, 'gt_guide_tip')))
slide_btn= driver.find_element_by_class_name('gt_guide_tip')
slide_btn.click()
image1= get_image(driver, '//div[@class="gt_cut_bg gt_show"]/div')
image2= get_image(driver, '//div[@class="gt_cut_fullbg gt_show"]/div')#圖片上 缺口的位置的x坐標(biāo)
#2 對(duì)比兩張圖片的所有RBG像素點(diǎn),得到不一樣像素點(diǎn)的x值,即要移動(dòng)的距離
l =get_distance(image1, image2)print('l=',l)#3 獲得移動(dòng)軌跡
track_list =get_track(l)print('第一步,點(diǎn)擊滑動(dòng)按鈕')
element= WebDriverWait(driver, 30, 0.5).until(EC.element_to_be_clickable((By.CLASS_NAME, 'gt_slider_knob')))
ActionChains(driver).click_and_hold(on_element=element).perform() #點(diǎn)擊鼠標(biāo)左鍵,按住不放
importtime
time.sleep(0.4)print('第二步,拖動(dòng)元素')for track intrack_list[0]:
ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() #鼠標(biāo)移動(dòng)到距離當(dāng)前位置(x,y)
#time.sleep(0.4)
for track in track_list[1]:
ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() #鼠標(biāo)移動(dòng)到距離當(dāng)前位置(x,y)
time.sleep(0.1)importtime
time.sleep(0.6)#ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform() # 鼠標(biāo)移動(dòng)到距離當(dāng)前位置(x,y)
#ActionChains(driver).move_by_offset(xoffset=8, yoffset=0).perform() # 鼠標(biāo)移動(dòng)到距離當(dāng)前位置(x,y)
#ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform() # 鼠標(biāo)移動(dòng)到距離當(dāng)前位置(x,y)
print('第三步,釋放鼠標(biāo)')
ActionChains(driver).release(on_element=element).perform()
time.sleep(1)defmain_check_slider(driver):"""檢查滑動(dòng)按鈕是否加載
:param driver:
:return:"""
whileTrue:try:
driver.get('https://www.huxiu.com/')
element= WebDriverWait(driver, 30, 0.5).until(EC.element_to_be_clickable((By.CLASS_NAME, 'js-login')))ifelement:returnelementexceptTimeoutException as e:print('超時(shí)錯(cuò)誤,繼續(xù)')
time.sleep(5)if __name__ == '__main__':try:
count= 3 #最多識(shí)別3次
driver =webdriver.Chrome()while count >0:#等待滑動(dòng)按鈕加載完成
element =main_check_slider(driver)
main_check_code(driver,element)try:
success_element= (By.CSS_SELECTOR, '.gt_success')#得到成功標(biāo)志
success_images = WebDriverWait(driver,3).until(EC.presence_of_element_located(success_element))ifsuccess_images:print('成功識(shí)別!!!!!!')
count=0importsys
sys.exit()exceptException as e:print('識(shí)別錯(cuò)誤,繼續(xù)')
count-= 1time.sleep(1)else:print('too many attempt check code')
exit('退出程序')finally:
driver.close()
總結(jié)
以上是生活随笔為你收集整理的python滑动验证码处理_python+selenium滑动式验证码解决办法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python drawline_基于py
- 下一篇: python类与继承person类_关于