python3 破解 geetest(極驗)的滑塊驗證碼功能,python3geetest

來源:互聯網
上載者:User

python3 破解 geetest(極驗)的滑塊驗證碼功能,python3geetest

下面一段代碼給大家介紹python破解geetest 驗證碼功能,具體代碼如下所示:

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.action_chains import ActionChainsimport PIL.Image as imageimport time,re, randomimport requeststry:  from StringIO import StringIOexcept 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#下載並還原圖片# driver:webdriver# div:圖片的divdef get_image(driver,div):  #找到圖片所在的div  background_images=driver.find_elements_by_xpath(div)  location_list=[]  imageurl=''  #圖片是被CSS按照位移的方式打亂的,我們需要找出這些位移,為後續還原做好準備  for background_image in background_images:    location={}    #在html裡面解析出小圖片的url地址,還有長高的數值    location['x']=int(re.findall("background-image: url\(\"(.*)\"\); background-position: (.*)px (.*)px;",background_image.get_attribute('style'))[0][1])    location['y']=int(re.findall("background-image: url\(\"(.*)\"\); background-position: (.*)px (.*)px;",background_image.get_attribute('style'))[0][2])    imageurl=re.findall("background-image: url\(\"(.*)\"\); background-position: (.*)px (.*)px;",background_image.get_attribute('style'))[0][0]    location_list.append(location)  #替換圖片的尾碼,獲得圖片的URL  imageurl=imageurl.replace("webp","jpg")  #獲得圖片的名字  imageName = imageurl.split('/')[-1]  #獲得圖片  session = requests.session()  r = session.get(imageurl, headers = headers, verify = False)  #下載圖片  with open(imageName, 'wb') as f:    f.write(r.content)    f.close()  #重新合并還原圖片  image=get_merge_image(imageName, location_list)  return image#對比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(0,260):    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():  #開啟Firefox瀏覽器  driver = webdriver.Firefox()  #用Firefox瀏覽器開啟網頁  driver.get("http://www.geetest.com/exp_embed")  #等待頁面的上元素重新整理出來  WebDriverWait(driver, 30).until(lambda the_driver: the_driver.find_element_by_xpath("//div[@class='gt_slider_knob gt_show']").is_displayed())  WebDriverWait(driver, 30).until(lambda the_driver: the_driver.find_element_by_xpath("//div[@class='gt_cut_bg gt_show']").is_displayed())  WebDriverWait(driver, 30).until(lambda the_driver: the_driver.find_element_by_xpath("//div[@class='gt_cut_fullbg gt_show']").is_displayed())  #下載圖片  image1=get_image(driver, "//div[@class='gt_cut_bg gt_show']/div")  image2=get_image(driver, "//div[@class='gt_cut_fullbg gt_show']/div")  #計算缺口位置  loc=get_diff_location(image1, image2)  #產生x的移動軌跡點  track_list=get_track(loc)  #找到滑動的圓球  element=driver.find_element_by_xpath("//div[@class='gt_slider_knob gt_show']")  location=element.location  #獲得滑動圓球的高度  y=location['y']  #滑鼠點擊元素並按住不放  print ("第一步,點擊元素")  ActionChains(driver).click_and_hold(on_element=element).perform()  time.sleep(0.15)  print ("第二步,拖動元素")  track_string = ""  for track in track_list:    #不能移動太快,否則會被認為是程式執行    track_string = track_string + "{%d,%d}," % (track, y - 445)    #xoffset=track+22:這裡的移動位置的值是相對於滑動圓球左上方的相對值,而軌跡變數裡的是圓球的中心點,所以要加上圓球長度的一半。    #yoffset=y-445:這裡也是一樣的。不過要注意的是不同的瀏覽器渲染出來的結果是不一樣的,要保證最終的計算後的值是22,也就是圓球高度的一半    ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=track+22, yoffset=y-445).perform()    #間隔時間也通過隨機函數來獲得,間隔不能太快,否則會被認為是程式執行    time.sleep(random.randint(10,50)/100)  print (track_string)  #xoffset=21,本質就是向後退一格。這裡退了5格是因為圓球的位置和滑動條的左邊緣有5格的距離  ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform()  time.sleep(0.1)  ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform()  time.sleep(0.1)  ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform()  time.sleep(0.1)  ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform()  time.sleep(0.1)  ActionChains(driver).move_to_element_with_offset(to_element=element, xoffset=21, yoffset=y-445).perform()  print ("第三步,釋放滑鼠")  #釋放滑鼠  ActionChains(driver).release(on_element=element).perform()  time.sleep(3)  #點擊驗證  # submit = driver.find_element_by_xpath("//div[@class='gt_ajax_tip success']")  # print(submit.location)  # time.sleep(5)  #關閉瀏覽器,為了示範方便,暫時注釋掉.  #driver.quit()#主函數入口if __name__ == '__main__':  pass  main()

總結

以上所述是小編給大家介紹的python3 破解 geetest(極驗)的滑塊驗證碼功能,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.