隋唐演义

隋唐演义

python自动识别滑动验证码 用python自动识别验证码完整教程

双十一 0

手机淘宝搜:天降红包55 5月20日开始,每天领红包。
京示搜:天降红包369,5月28日开始

python培训好的机构,python求素数,python怎么读,python编程
【下载文档:  python验证码识别教程之滑动验证码.txt 】
python验证码识别教程之滑动验证码前言上篇文章记录了2种分割验证码的方法,此外还有一种叫做”滴水算法”(Drop Fall Algorithm)的方法,但本人智商原因看这个算法看的云里雾里的,所以今天记录滑动验证码的处理吧。网上据说有大神已经破解了滑动验证码的算法,可以不使用selenium来破解,但本人能力不足还是使用笨方法吧。基础原理很简单,首先点击验证码按钮后的图片是滑动后的完整结果,点击一下滑块后会出现拼图,对这2个分别截图后比较像素值来找出滑动距离,并结合selenium来实现拖拽效果。至于selenium怎么安装就不说了,滑动验证码的一个难点就是要模拟人的拖拽行为,移动快了不行,慢了也不行。这里以国家企业公示网站为例:# -*- coding: utf-8 -*-import timeimpo【【微信】】from io import BytesIOfrom PIL import Imagefrom selenium import webdri【【微信】】.【【微信】】.common.by import Byfrom selenium.webdri【【微信】】sfrom selenium.【【微信】】.【【微信】】.ui import WebDri【【微信】】from selenium.【【微信】】.【【微信】】 import expected_conditions as ECclass Slide(object):"""滑动验证码破解"""def __init__(self, target):self.target=target # 要搜索的公司名称self.driver=【【微信】】.Chrome()self.wait=WebDri【【微信】】(self.driver, 10)def crop(self, left, top, right, bottom, 【【微信】】):"""截屏并裁剪"""ss=Image.open(BytesIO(self.driver.【【微信】】g()))cp=ss.crop((left, top, right, bottom)) # 注意这里顺序cp.save(【【微信】】)return cpdef 【【微信】】lf, pic1, pic2):"""根据阈值计算移动距离"""pix1=pic1.load()pix2=pic2.load()threshold=200move=0# 因为滑块都从左向右滑动,而碎片本身宽度为60所以从60开始遍历for i in range(60, pic1.size[0]):flag=Falsefor j in range(pic1.size[1]):r=abs(pix1[i, j][0] - pix2[i, j][0])g=abs(pix1[i, j][1] - pix2[i, j][1])b=abs(pix1[i, j][2] - pix2[i, j][2])# if r > threshold and g > threshold and b > threshold:# 方法1:分别判断rgb大于阈值# flag=True# breakif r + g + b > threshold:# 方法2:判断rgb总和跟阈值比较,效果比1好 为什么呢??flag=Truebreakif flag:move=【【微信】】ath1(self, 【【微信】】):"""绘制移动路径方法1,构造一个等比数列"""q=0.4 # 测试后发现0.4效果最佳n=10 # 最多移动几次a1=((1 - q) * 【【微信】】) / (1 - q**n)【【微信】】=[]for o in range(1, n + 1):an=a1 * q**(o - 1)if an < 0.1: # 小于移动阈值的就不要了breakt=【【微信】】.uniform(0, 0.5) # 测试后0.5秒的间隔成功率最高【【微信】】.append([an, 0, t])return 【【微信】】def path2(self, 【【微信】】):"""绘制移动路径方法2,模拟物理加速、减速运动,效果比1好"""【【微信】】=[]current=0# 减速阈值mid=【【微信】】 * 4 / 5# 计算间隔t=0.2# 初速度v=0【【微信】】 < (【【微信】】 - 10):if current < mid:# 加速度为正2a=2else:# 加速度为负3a=-3# 初速度v0v0=v# 当前速度v=v0 + atv=v0 + a * t# 移动距离x=v0t + 1/2 * a * t^2move=v0 * t + 0.5 * a * t * t# 当前位移current +=move# 加入轨迹【【微信】】.append([round(move), 0, 【【微信】】.uniform(0, 0.5)])return 【【微信】】def run(self):self.driver.get("http://www.gsxt.gov.cn/index")【【微信】】=self.driver.【【微信】】('keyword')【【微信】】.send_keys(self.target)search_btn=self.driver.【【微信】】('btn_query')time.sleep(3) # 注意这里等一下再点,否则会出现卡死现象search_btn.click()# 等待验证码弹出bg_pic=self.wait.until(EC.【【淘密令】】_of_element_located((By.CLASS_NAME,"gt_cut_fullbg")))# html中坐标原点是左上角,右为x轴正方向,下为y轴正方向# 输出的x为正就是此元素距离屏幕左侧距离# 输出的y为正就是此元素距离屏幕上侧距离# 所以我们需要截图的四个距离如下:top, bottom, left, right=(bg_pic.location['y'], bg_pic.location['y'] + bg_pic.size['height'],bg_pic.location['x'], bg_pic.location['x'] + bg_pic.size['width'])time.sleep(1)cp1=self.crop(left, top, right, bottom, '1.png')# 获取滑块按钮并点击一下slide=self.wait.until(EC.【【淘密令】】_of_element_located((By.CLASS_NAME,"gt_slider_knob")))slide.click()time.sleep(3) # 等3秒报错信息消失 TODO 这里应该可以改进cp2=self.crop(left, top, right, bottom, '2.png')move=self.【【微信】】(cp1, cp2)【【微信】】=self.path1(move)# 【【微信】】=self.path2(move)# 拖动滑块【【微信】】elf.driver).【【微信】】(slide).perform()for x in 【【微信】】:【【微信】】elf.driver).【【微信】】(xoffset=x[0],yoffset=x[1]).perform()# 【【微信】】(driver).【【微信】】_offset(to_element=slide,xoffset=x[0],yoffset=x[1]).perform()time.sleep(x[-1]) # 如果使用方法1则需要sleeptime.sleep(0.5)【【微信】】elf.drive【【微信】】).perform() # 释放按钮time.sleep(0.8)element=self.wait.until(EC.【【淘密令】】_of_element_located((By.CLASS_NAME, "gt_info_text")))ans=element.textif u"通过" in ans:# 这里也需要等一下才能获取到具体的链接element=self.wait.until(EC.【【淘密令】】_of_all_elements_located((By.CLASS_NAME, "search_list_item")))for o in self.driver.【【微信】】th(u"http://a[@target='_blank']"):print(o.get_attribute("href"))self.driver.quit()else:print("识别失败")self.driver.quit()【【微信】】_main__':s=Slide('中国平安')s.run()代码中注释很详细就不多说了,如果运行时候提示【【网址】】mon.exceptions.WebDri【【微信】】: Message: 'chromedriver' 【【微信】】n PATH. Please see 【【网址】】/chromedriver/home则需要到 【【网址】】/chromedriver/home 下载驱动后解压到/usr/local/bin目录即可。使用服务器运行时使用phantomjs替换chrome,另外失败的时候可以进行判断自动重试,有兴趣的小伙伴可以自己补充完善。总结以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对中文源码网的支持。

python3 验证码 python滑块验证技术

python能做什么,python入门教程(非常详细),python编程,python安装教程
【下载文档:  python3 破解 【【微信】】(极验)的滑块验证码功能.txt 】
python3 破解 【【微信】】(极验)的滑块验证码功能下面一段代码给大家介绍python破解【【微信】】 验证码功能,具体代码如下所示:from selenium import webdri【【微信】】.【【微信】】.support.ui import WebDri【【微信】】.【【微信】】.common.action_chains import ActionChainsimport PIL.Image as imageimport time,re, randomimport re【【微信】】:from StringIO import StringIOexcept ImportError:from io import StringIO#爬虫模拟的浏览器头部信息agent='Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/【【QQ微信】】 【【微信】】/33.0'headers={'User-Agent': agent}# 根据位置对图片进行合并还原# 【【淘密令】】:图片# 【【微信】】:图片位置#内部两个图片处理函数的介绍#crop函数带的参数为(起始点的横坐标,起始点的纵坐标,宽度,高度)#paste函数的参数为(需要修改的图片,粘贴的起始点的横坐标,粘贴的起始点的纵坐标)def get_merge_image(【【淘密令】】,【【微信】】):#打开图片文件im=image.open(【【淘密令】】)#创建新的图片,大小为260*116new_im=image.new('RGB', (260,116))im_list_upper=[]im_list_down=[]# 拷贝图片for location in 【【微信】】:#上面的图片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=0for im in im_list_down:new_im.paste(im, (x_offset,58))x_offset +=im.size[0]return new_im#下载并还原图片# driver:【【微信】】# div:图片的【【微信】】(driver,div):#找到图片所在的di【【微信】】=driver.【【微信】】h(div)【【微信】】=[]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]【【微信】】.append(location)#替换图片的后缀,获得图片的URLimageurl=imageurl.replace("webp","jpg")#获得图片的名字imageName=imageurl.split('/')[-1]#获得图片session=re【【微信】】ession()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, 【【微信】】)return image#对比RGB值def is_similar(image1,【【微信】】,x,y):pass#获取指定位置的RGB值pixel1=image1.getpixel((x,y))pixel2=【【微信】】.getpixel((x,y))for i in range(0,3):# 如果相差超过50则就认为找到了缺口的位置if abs(pixel1[i]-pixel2[i])>=50:return Falsereturn True#计算缺口的位置def get_diff_location(image1,【【微信】】):i=0# 两张原始图的大小都是相同的260*116# 那就通过两个for循环依次对比每个像素点的RGB值# 如果相差超过50则就认为找到了缺口的位置for i in range(0,260):for j in range(0,116):if is_similar(image1,【【微信】】,i,j)==False:return i#根据缺口的位置模拟x轴移动的轨迹def get_track(length):passlist=[]#间隔通过随机范围函数来获得,每次移动一步或者两步x=random.randint(1,3)#生成轨迹并保存到list内while length-x>=5:list.append(x)length=length-xx=random.randint(1,3)#最后五步都是一步步移动for i in range(length):list.append(1)return list#滑动验证码破解程序def main():#打开火狐浏览器driver=【【微信】】.【【微信】】()#用火狐浏览器打开网页driver.get("http://www.【【微信】】.com/exp_embed")#等待页面的上元素刷新出来WebDri【【微信】】(driver, 30).until(lambda 【【微信】】: 【【微信】】.【【微信】】h("http://div[@class='【【微信】】w']").is_displayed())WebDri【【微信】】(driver, 30).until(lambda 【【微信】】: 【【微信】】.【【微信】】h("http://div[@class='【【微信】】']").is_displayed())WebDri【【微信】】(driver, 30).until(lambda 【【微信】】: 【【微信】】.【【微信】】h("http://div[@class='【【微信】】']").is_displayed())#下载图片image1=get_image(driver, "http://div[@class='【【微信】】']/div")【【微信】】=get_image(driver, "http://div[@class='【【微信】】']/div")#计算缺口位置loc=get_diff_location(image1, 【【微信】】)#生成x的移动轨迹点track_list=get_track(loc)#找到滑动的圆球element=driver.【【微信】】h("http://div[@class='【【微信】】w']")location=element.location#获得滑动圆球的高度y=location['y']#鼠标点击元素并按住不放print ("第一步,点击元素")ActionChains(driver).【【微信】】(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).【【微信】】_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).【【微信】】_offset(to_element=element, xoffset=21, yoffset=y-445).perform()time.sleep(0.1)ActionChains(driver).【【微信】】_offset(to_element=element, xoffset=21, yoffset=y-445).perform()time.sleep(0.1)ActionChains(driver).【【微信】】_offset(to_element=element, xoffset=21, yoffset=y-445).perform()time.sleep(0.1)ActionChains(driver).【【微信】】_offset(to_element=element, xoffset=21, yoffset=y-445).perform()time.sleep(0.1)ActionChains(driver).【【微信】】_offset(to_element=element, xoffset=21, yoffset=y-445).perform()print ("第三步,释放鼠标")#释放鼠标ActionChains(drive【【微信】】(on_element=element).perform()time.sleep(3)#点击验证# submit=driver.【【微信】】h("http://div[@class='【【微信】】']")# print(submit.location)# time.sleep(5)#关闭浏览器,为了演示方便,暂时注释掉.#driver.quit()#主函数入口if __name__=='__main__':【【淘密令】】()总结以上所述是小编给大家介绍的python3 破解 【【微信】】(极验)的滑块验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对中文源码网网站的支持!