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

python破解bilibili滑动验证码登录功能

Python  /  管理员 发布于 7年前   217

地址:https://passport.bilibili.com/login

左图事完整验证码图,右图是有缺口的验证码图

                                                                    

步骤:

1.准备bilibili账号

2.工具:pycharm selenium chromedriver PIL

3.破解思路:

找到完整验证码和有缺口的验证码图片,然后计算缺口坐标,再利用selenium移动按钮到指定位置,齐活

步骤代码如下:

先导入需要的包和库

from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byfrom selenium.webdriver import ActionChainsfrom urllib.request import urlretrievefrom bs4 import BeautifulSoupfrom lxml.html import etreeimport re,requestsfrom PIL import Imagefrom time import sleepfrom .config import username,passwordclass Jiyan_test:  def __init__(self):    self.url='https://passport.bilibili.com/login'    self.brower=webdriver.Chrome('chromedriver')    self.wait=WebDriverWait(self.brower,20)#设置等待   def login(self):    self.brower.get(self.url)    self.user=self.wait.until(EC.presence_of_element_located((By.ID,'login-username')))    self.passwd=self.wait.until(EC.presence_of_element_located((By.ID,'login-passwd')))    self.user.send_keys(username)    self.passwd.send_keys(password)  def get_images(self):#获取验证码图片    # print(self.brower.page_source)    full_position=[]#完整散图坐标    bg_position=[]#缺口散图坐标    html=etree.HTML(self.brower.page_source)    gt_cut_fullbg_slices=html.xpath('//div[@class="gt_cut_fullbg_slice"]/@style')    full_slice_url=re.findall('url\(\"(.*)\"\);',gt_cut_fullbg_slices[0])[0].replace('webp','jpg')    gt_cut_bg_slices = html.xpath('//div[@class="gt_cut_bg_slice"]/@style')    bg_slice_url = re.findall('url\(\"(.*)\"\);', gt_cut_bg_slices[0])[0].replace('webp', 'jpg')    print(gt_cut_fullbg_slices)    for i in gt_cut_fullbg_slices:      position=re.findall('background-position: (.*);',i)[0].replace('px','').split(' ')      position=[int(i) for i in position]      full_position.append(position)    for i in gt_cut_fullbg_slices:      position = re.findall('background-position: (.*);', i)[0].replace('px','').split(' ')      position=[int(i) for i in position]      bg_position.append(position)    print(full_position)    print(bg_position)    print(full_slice_url)    print(bg_slice_url)    full_pic_data=requests.get(full_slice_url).content    bg_pic_data=requests.get(bg_slice_url).content    with open('image/full_pic.jpg','wb') as f:      f.write(full_pic_data)    with open('image/bg_pic.jpg', 'wb') as f:      f.write(bg_pic_data)    full_image=Image.open('image/full_pic.jpg')    bg_image=Image.open('image/bg_pic.jpg')    return full_image,bg_image,full_position,bg_position

分割图片 

def pic_cut(self,file,position):#分割图片    first_line_pic=[]    second_line_pic=[]    # full_image, bg_image, full_position, bg_position=self.get_images()    for p in position:      if p[1]==-58:        first_line_pic.append(file.crop((abs(p[0]),58,abs(p[0])+10,166)))      if p[1]==0:        second_line_pic.append(file.crop((abs(p[0]),0,abs(p[0])+10,58)))    print(first_line_pic)    print(second_line_pic)    return first_line_pic,second_line_pic

合并图片

   

 def merge_pics_new(self,first_line_pic,second_line_pic,file_name):    #新建图片    image=Image.new('RGB',(260,116))    offset=0#设置偏移量    #拼接第一行    for i in first_line_pic:      image.paste(i,(offset,0))      offset+=i.size[0]    offset_x=0    #拼接第二行    for j in second_line_pic:      image.paste(j,(offset_x,58))      offset_x+=j.size[0]    image.save('image/'+file_name)#合成完整图片   def merge_pics(self):#合并图片    #先割切乱码图片    full_image, bg_image, full_position, bg_position=self.get_images()    first_line_pic, second_line_pic=self.pic_cut(full_image,full_position)    self.merge_pics_new(first_line_pic, second_line_pic,'full_new_pic.jpg')    first_line_pic, second_line_pic = self.pic_cut(bg_image, bg_position)    self.merge_pics_new(first_line_pic, second_line_pic, 'bg_new_pic.jpg')

再判断图片是否一样

  def check_pics_is_same(self,bg_image,full_image,x,y):#判断图片是否一样    bg_pixel=bg_image.getpixel((x,y))    full_pixel=full_image.getpixel((x,y))    for i in range(0,3):      if abs(bg_pixel[i]-full_pixel[i])>=50:        return False      else:        return True

计算滑块距离

   

def reckon_distance2(self):#计算滑块    try:      full_image = Image.open('image/full_new_pic.jpg')      bg_image = Image.open('image/bg_new_pic.jpg')      for i in range(0,full_image.size[0]):        for j in range(0,full_image.size[1]):          if not self.check_pics_is_same(bg_image,full_image,i,j):return i    except Exception:      print('图片读取失败')

计算运动轨迹

    

def reckon_trail(self):#计算运动轨迹    print('计算运动轨迹')    track=[]    distance=self.reckon_distance2()    distance=int(distance)-7#矫正值    print('缺口坐标',distance)    fast_distance=distance*(4/5)    start,v0,t=0,0,0.2    while start<distance:      if start<fast_distance:#加速状态        a=1.5#加速      else:        a=-3#减速      #数学公式 s=v0*t+1/2 v*t平方      move=v0*t+1/2*a*t*t      #当前速度      v=v0+a*t      #重置粗速度      v0=v      #重置起始位置      start+=move      track.append(round(move))    return track

 

模拟拖动

def move_block(self):# 模拟拖动滑块    print('开始模拟')    track=self.reckon_trail()    #找寻滑块标签    slider=self.wait.until(EC.presence_of_element_located((By.CLASS_NAME,'gt_slider_knob')))    ActionChains(self.brower).click_and_hold(slider).perform()#执行    for x in track:      ActionChains(self.brower).move_by_offset(xoffset=x,yoffset=0).perform()    sleep(0.4)    ActionChains(self.brower).release().perform()#释放滑块 if __name__ == '__main__':  c=Jiyan_test()  c.login()  c.merge_pics()  c.move_block()

测试运行正常,偶尔有对不准的现象,需要调整distance的值

总结

以上所述是小编给大家介绍的python破解bilibili滑动验证码登录功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


  • 上一条:
    使用Python为中秋节绘制一块美味的月饼
    下一条:
    python修改FTP服务器上的文件名
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

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

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

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

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

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

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

    侯体宗的博客