侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

使用Selenium破解新浪微博的四宫格验证码

技术  /  管理员 发布于 7年前   207

在我们爬虫的时候经常会遇到验证码,新浪微博的验证码是四宫格形式。

可以采用模板验证码的破解方式,也就是把所有验证码的情况全部列出来,然后拿验证码的图片和这所有情况中的图片进行对比,然后获取验证码,再通过selenium自动拖拽点击,进行破解。

我们将验证码四个点标注为1234,那么所有的情况就是以下24种情况。

数字代表箭头指向:

1234 2134 3124 4321
1243 2143 3142 4312
1342 2314 3214 4123
1324 2341 3241 4132
1423 2413 3412 4213
1432 2431 3421 4231

所有的情况就是以上24种。我们将这24中验证码的情况放在一个文件夹内,当我们在登录的时候用获取的验证码截图去和所有的情况一一对比,然后获取完全相同的验证码,进行点击即可。代码如下:

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.common.action_chains import ActionChainsimport timefrom PIL import Imagefrom io import BytesIOfrom os import listdirUSERNAME = ''PASSWORD = ''class CrackWeiboSlide():  def __init__(self):    self.url = 'https://passport.weibo.cn/signin/login'    self.browser = webdriver.Chrome()    self.wait = WebDriverWait(self.browser,20)    self.username = USERNAME    self.password = PASSWORD  def __del__(self):    self.browser.close()  def open(self):    """    打开网页输入用户名密码登录    :return: None    """    self.browser.get(self.url)    username = self.wait.until(EC.presence_of_element_located((By.ID,'loginName')))    password = self.wait.until(EC.presence_of_element_located((By.ID,'loginPassword')))    submit = self.wait.until(EC.element_to_be_clickable((By.ID, 'loginAction')))    username.send_keys(self.username)    password.send_keys(self.password)    submit.click()  def get_position(self):    """    获取验证码的位置    :return: 位置    """    try:      img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'patt-shadow')))    except TimeoutException:      print('未出现验证码')      self.open()    time.sleep(2)    location = img.location    size = img.size    top=location['y']    bottom = location['y']+size['height']    left = location['x']    right = location['x']+size['width']    return (top,bottom,left,right)  def get_screenshot(self):    """    获取截图    :return:截图    """    screentshot = self.browser.get_screenshot_as_png()    # BytesIO将网页截图转换成二进制    screentshot = Image.open(BytesIO(screentshot))    return screentshot  def get_image(self,name):    """获取验证码图片"""    top,bottom,left,right = self.get_position()    print('验证码位置',top,bottom,left,right)    screenshot = self.get_screenshot()    # crop()将图片裁剪出来,后面需要一个参数    captcha = screenshot.crop((left,top,right,bottom))    captcha.save(name)    return captcha  def detect_image(self,image):    """    匹配图片    :param self:    :param image: 图片    :return: 拖动顺序    """    # 图片所在的文件夹    for template_name in listdir('templates/'):      print('正在匹配',template_name)      template = Image.open('templates/'+template_name)      # 匹配图片      if self.same_img(image,template):        # 将匹配到的文件名转换为列表        numbers = [int(number)for number in list(template_name.split('.')[0])]        print('拖动顺序',numbers)        return numbers  def is_pixel_equal(self,image1,image2,x,y):    """    判断两个像素的相似度    :param image1: 图片1    :param image2: 图片2    :param x: 位置x    :param y: 位置y    :return: 像素是否相同    """     # 取像素点    pixel1 = image1.load()[x,y]    pixel2 = image2.load()[x,y]    # 偏差量等于60    threshold = 60    if abs(pixel1[0]-pixel2[0]) < threshold and abs(pixel1[1]-pixel2[1])<threshold and abs(pixel1[2]-pixel2[2])<threshold:      return True    else:      return False  def same_img(self,image,template):    """    识别相似的验证码    :param image: 准备识别的验证码    :param template: 模板    :return:    """    # 相似度阈值    threshold = 0.99    count = 0    # 匹配所有像素点    for x in range(image.width):      for y in range(image.height):        # 判断像素        if self.is_pixel_equal(image,template,x,y):          count+=1    result = float(count)/(image.width*image.height)    if result>threshold:      print('成功匹配')      return True    return False  def move(self,numbers):    """    根据顺序拖动,此处接收的参数为前面的验证码的顺序列表    :param numbers:    :return:    """    # 获取四宫格的四个点    circles = self.browser.find_elements_by_css_selector('.patt-wrap .patt-circ')    print('-----------------',circles)    dx = dy =0    for index in range(4):      circle = circles[numbers[index]-1]      if index == 0:        # 点击第一个点        ActionChains(self.browser).move_to_element_with_offset(circle,circle.size['width']/2,circle.size['height']/2).click_and_hold().perform()      else:        # 慢慢移动        times = 30        for i in range(times):          ActionChains(self.browser).move_by_offset(dx/times,dy/times).perform()          time.sleep(1/times)      if index == 3:        # 松开鼠标        ActionChains(self.browser).release().perform()      else:        # 计算下次的偏移        dx = circles[numbers[index+1]-1].location['x'] - circle.location['x']        dy = circles[numbers[index+1]-1].location['y'] - circle.location['y']  def crack(self):    """    破解入口    :return:    """    self.open()    # 获取验证码图片    image = self.get_image('captcha.png')    numbers = self.detect_image(image)    self.move(numbers)    time.sleep(10)    print('识别结束')if __name__ == '__main__':  crack = CrackWeiboSlide()  crack.crack()

设置自己的账号密码即可实现。

有时候会匹配不上,图片相似度阈值达不到0.99以上,这个时候可能是我们收集的验证码图片过时了,重新开启图片收集程序,运行收集一下即可。

收集图片程序代码如下:

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import TimeoutExceptionimport timefrom PIL import Imagefrom io import BytesIOfrom os import listdirUSERNAME = '18239831004'PASSWORD = 'qweqweqwe'class CrackWeiboSlide():  def __init__(self):    self.url = 'https://passport.weibo.cn/signin/login'    self.browser = webdriver.Chrome()    self.wait = WebDriverWait(self.browser,20)    self.username = USERNAME    self.password = PASSWORD  def __del__(self):    self.browser.close()  def open(self):    """    打开网页输入用户名密码登录    :return: None    """    self.browser.get(self.url)    username = self.wait.until(EC.presence_of_element_located((By.ID,'loginName')))    password = self.wait.until(EC.presence_of_element_located((By.ID,'loginPassword')))    submit = self.wait.until(EC.element_to_be_clickable((By.ID, 'loginAction')))    username.send_keys(self.username)    password.send_keys(self.password)    submit.click()  def get_position(self):    """    获取验证码的位置    :return: 位置    """    try:      img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'patt-shadow')))    except TimeoutException:      print('未出现验证码')      self.open()    time.sleep(2)    location = img.location    size = img.size    top=location['y']    bottom = location['y']+size['height']    left = location['x']    right = location['x']+size['width']    return (top,bottom,left,right)  def get_screenshot(self):    """    获取截图    :return:截图    """    screentshot = self.browser.get_screenshot_as_png()    # BytesIO将网页截图转换成二进制    screentshot = Image.open(BytesIO(screentshot))    return screentshot  def get_image(self,name):    """获取验证码图片"""    top,bottom,left,right = self.get_position()    print('验证码位置',top,bottom,left,right)    screenshot = self.get_screenshot()    # crop()将图片裁剪出来,后面需要一个参数    captcha = screenshot.crop((left,top,right,bottom))    captcha.save(name)    return captcha  # 获取所有的验证码  def main(self):    count = 0    while True:      name = str(count)+'.png'      self.open()      self.get_image(name)      count+=1if __name__ == '__main__':  crack = CrackWeiboSlide()  crack.main()

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对AIDI的支持。如果你想了解更多相关内容请查看下面相关链接


  • 上一条:
    Tesserocr库的正确安装方式
    下一条:
    Selenium的使用详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客