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采集百度百科的方法
Jun 05 Python
python比较两个列表是否相等的方法
Jul 28 Python
python 捕获 shell/bash 脚本的输出结果实例
Jan 04 Python
python实现集中式的病毒扫描功能详解
Jul 09 Python
Python人工智能之路 之PyAudio 实现录音 自动化交互实现问答
Aug 13 Python
TensorFlow加载模型时出错的解决方式
Feb 06 Python
重写django的model下的objects模型管理器方式
May 15 Python
python的flask框架难学吗
Jul 31 Python
Python gevent协程切换实现详解
Sep 14 Python
Python+OpenCV检测灯光亮点的实现方法
Nov 02 Python
python中time.ctime()实例用法
Feb 03 Python
PyQt5结合QtDesigner实现文本框读写操作
Jun 11 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
PHP4中实现动态代理
2006/10/09 PHP
国产PHP开发框架myqee新手快速入门教程
2014/07/14 PHP
如何让thinkphp在模型中自动完成session赋值小教程
2014/09/05 PHP
php+ajax无刷新上传图片的实现方法
2016/12/06 PHP
php读取出一个文件夹及其子文件夹下所有文件的方法示例
2017/06/15 PHP
jQuery 跨域访问问题解决方法
2009/12/02 Javascript
漂亮的jquery提示效果(仿腾讯弹出层)
2013/02/05 Javascript
事件委托与阻止冒泡阻止其父元素事件触发
2014/09/02 Javascript
让html页面不缓存js的实现方法
2014/10/31 Javascript
Javascript中的Callback方法浅析
2015/03/15 Javascript
详解nodejs中的process进程
2017/03/19 NodeJs
详解Vue中一种简易路由传参办法
2017/09/15 Javascript
浅谈JavaScript的innerWidth与innerHeight
2017/10/12 Javascript
Angular4.0中引入laydate.js日期插件的方法教程
2017/12/25 Javascript
基于vue中css预加载使用sass的配置方式详解
2018/03/13 Javascript
Vue点击切换颜色的方法
2018/09/13 Javascript
详解vue几种主动刷新的方法总结
2019/02/19 Javascript
JavaScript 实现轮播图特效的示例
2020/11/05 Javascript
Python实现Const详解
2015/01/27 Python
讲解Python中fileno()方法的使用
2015/05/24 Python
Python装饰器使用实例:验证参数合法性
2015/06/24 Python
简单实现python数独游戏
2018/03/30 Python
python使用selenium登录QQ邮箱(附带滑动解锁)
2019/01/23 Python
解决Python内层for循环如何break出外层的循环的问题
2019/06/24 Python
Python *args和**kwargs用法实例解析
2020/03/02 Python
Python unittest框架操作实例解析
2020/04/13 Python
详解Python中namedtuple的使用
2020/04/27 Python
python asyncio 协程库的使用
2021/01/21 Python
布鲁明戴尔百货店:Bloomingdale’s
2016/12/21 全球购物
美国领先的水果篮送货公司和新鲜水果供应商:The Fruit Company
2018/02/13 全球购物
质量月活动策划方案
2014/03/10 职场文书
分层教学实施方案
2014/03/19 职场文书
农村党支部承诺书
2015/04/30 职场文书
员工手册董事长致辞
2015/07/29 职场文书
DBCA命令行搭建Oracle ADG的流程
2021/06/11 Oracle
SpringBoot2 参数管理实践之入参出参与校验的方式
2021/06/16 Java/Android