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


Posted in Python onSeptember 11, 2019

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

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

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

步骤:

1.准备bilibili账号

2.工具:pycharm selenium chromedriver PIL

3.破解思路:

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

步骤代码如下:

先导入需要的包和库

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from urllib.request import urlretrieve
from bs4 import BeautifulSoup
from lxml.html import etree
import re,requests
from PIL import Image
from time import sleep
from .config import username,password
class 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 fabric使用笔记
May 09 Python
Python使用pylab库实现画线功能的方法详解
Jun 08 Python
Python数据结构与算法之图的广度优先与深度优先搜索算法示例
Dec 14 Python
详解django中使用定时任务的方法
Sep 27 Python
在python 中split()使用多符号分割的例子
Jul 15 Python
Python实现决策树并且使用Graphviz可视化的例子
Aug 09 Python
Python 项目转化为so文件实例
Dec 23 Python
Python 模拟动态产生字母验证码图片功能
Dec 24 Python
Pytorch mask-rcnn 实现细节分享
Jun 24 Python
Jupyter安装拓展nbextensions及解决官网下载慢的问题
Mar 03 Python
解决Jupyter-notebook不弹出默认浏览器的问题
Mar 30 Python
这样写python注释让代码更加的优雅
Jun 02 Python
python修改FTP服务器上的文件名
Sep 11 #Python
解析python实现Lasso回归
Sep 11 #Python
Python 点击指定位置验证码破解的实现代码
Sep 11 #Python
python实现的接收邮件功能示例【基于网易POP3服务器】
Sep 11 #Python
python实现的发邮件功能示例
Sep 11 #Python
python 字符串常用函数详解
Sep 11 #Python
python sqlite的Row对象操作示例
Sep 11 #Python
You might like
php 无极分类(递归)实现代码
2010/01/05 PHP
php 模拟 asp.net webFrom 按钮提交事件的思路及代码
2013/12/02 PHP
centos 7.2下搭建LNMP环境教程
2016/11/20 PHP
JS的数组的扩展实例代码
2008/07/09 Javascript
style、 currentStyle、 runtimeStyle区别分析
2010/08/01 Javascript
url地址自动加#号问题说明
2010/08/21 Javascript
JQUERY的属性选择符和自定义选择符使用方法(二)
2011/04/07 Javascript
使用基于jquery的gamequery插件做JS乒乓球游戏
2011/07/31 Javascript
javascript中typeof的使用示例
2013/12/19 Javascript
jQuery操作属性和样式详解
2016/04/13 Javascript
Jqprint实现页面打印
2017/01/06 Javascript
浅谈node的事件机制
2017/10/09 Javascript
Node.js中的cluster模块深入解读
2018/06/11 Javascript
JavaScript实现省市区三级联动
2020/02/13 Javascript
JavaScript中while循环的基础使用教程
2020/08/11 Javascript
[06:53]DOTA2每周TOP10 精彩击杀集锦vol.3
2014/06/25 DOTA
c++生成dll使用python调用dll的方法
2014/01/20 Python
Python新手实现2048小游戏
2015/03/31 Python
进一步理解Python中的函数编程
2015/04/13 Python
在Python中使用第三方模块的教程
2015/04/27 Python
使用FastCGI部署Python的Django应用的教程
2015/07/22 Python
python字典多键值及重复键值的使用方法(详解)
2016/10/31 Python
利用Python开发实现简单的记事本
2016/11/15 Python
Python排序搜索基本算法之堆排序实例详解
2017/12/08 Python
Python文件常见操作实例分析【读写、遍历】
2018/12/10 Python
Python使用requests提交HTTP表单的方法
2018/12/26 Python
python-itchat 统计微信群、好友数量,及原始消息数据的实例
2019/02/21 Python
pycharm中如何自定义设置通过“ctrl+滚轮”进行放大和缩小实现方法
2020/09/16 Python
一款恶搞头像特效的制作过程 利用css3和jquery
2014/11/21 HTML / CSS
全球最大的跑步用品商店:Road Runner Sports
2016/09/11 全球购物
房地产出纳岗位职责
2013/12/01 职场文书
护士岗前培训自我评鉴
2014/02/28 职场文书
小学班长竞选演讲稿
2014/04/24 职场文书
2015年化工厂工作总结
2015/05/04 职场文书
大学迎新生欢迎词
2015/09/29 职场文书
MySQL查询日期时间
2022/05/15 MySQL