Python3 crack geetest (polar inspection) of the slider verification code

Source: Internet
Author: User
Tags abs

From selenium import Webdriver
From Selenium.webdriver.support.ui import webdriverwait
From Selenium.webdriver.common.action_chains import Actionchains
Import PIL. Image as Image
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
}

# merge and restore pictures based on location
# FileName: Image
# location_list: Picture location
#内部两个图片处理函数的介绍
#crop函数带的参数为 (The horizontal axis of the starting point, the ordinate, width, height) of the starting point
#paste函数的参数为 (need to modify the image, paste the starting point of the horizontal axis, paste the starting point of the ordinate)
def get_merge_image (filename,location_list):
#打开图片文件
im = Image.open (filename)
#创建新的图片, size is 260*116
New_im = image.new (' RGB ', (260,116))
Im_list_upper=[]
Im_list_down=[]
# Copy Pictures
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: Picture of Div
def get_image (Driver,div):
#找到图片所在的div
Background_images=driver.find_elements_by_xpath (DIV)
Location_list=[]
Imageurl= "
#图片是被CSS按照位移的方式打乱的, we need to find these displacements and prepare for subsequent restores.
For Background_image in Background_images:
location={}
#在html里面解析出小图片的url地址, and a long, high value.
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)
#替换图片的后缀, get the URL of the picture
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):
# If the difference is more than 50, the position of the gap will be identified.
If ABS (Pixel1[i]-pixel2[i]) >=50:
Return False
Return True

#计算缺口的位置
def get_diff_location (Image1,image2):
I=0
# The size of the two original graphs is the same 260*116
# then compare the RGB values of each pixel with two for loops in turn.
# If the difference is more than 50, the position of the gap will be identified.
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=[]
#间隔通过随机范围函数来获得, move one or two steps at a time
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 ("http://www.geetest.com/exp_embed")
#等待页面的上元素刷新出来
Webdriverwait (driver, +). Until (Lambda The_driver:the_driver.find_element_by_xpath ("//div[@class = ' Gt_slider_knob Gt_show '] "). Is_displayed ())
Webdriverwait (driver, +). Until (Lambda The_driver:the_driver.find_element_by_xpath ("//div[@class = ' Gt_cut_bg gt_ Show '] "). Is_displayed ())
Webdriverwait (driver, +). 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 ("First step, click Element")
Actionchains (Driver). Click_and_hold (On_element=element). Perform ()
Time.sleep (0.15)
Print ("Second step, drag element")
track_string = ""
For track in Track_list:
#不能移动太快, otherwise it would be considered a program execution
track_string = track_string + "{%d,%d},"% (track, y-445)
#xoffset =track+22: The value of the moving position is relative to the upper-left corner of the sliding sphere, and the locus variable is the center of the ball, so add half the length of the sphere.
#yoffset =y-445: The same is true here. However, it is important to note that different browsers render the result is not the same, to ensure that the final calculated value is 22, that is, half the height of the ball
Actionchains (Driver). Move_to_element_with_offset (To_element=element, xoffset=track+22, yoffset=y-445). Perform ()
#间隔时间也通过随机函数来获得, the interval cannot be too fast, otherwise it will be considered a program execution
Time.sleep (Random.randint (10,50)/100)
Print (track_string)
#xoffset = 21, the essence is to back one lattice. This is a 5-cell retreat because the ball's position and the left edge of the slider are 5 bars away.
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 ("Step three, release the Mouse")
#释放鼠标
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)
#关闭浏览器, for illustrative purposes, temporarily commented out.
#driver. Quit ()

#主函数入口
if __name__ = = ' __main__ ':
Pass
Main ()

Python3 crack geetest (polar inspection) of the slider verification code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.