Python爬虫破解登陆哔哩哔哩的方法


Posted in Python onNovember 17, 2020

写在前面

作为一名找不到工作的爬虫菜鸡人士来说,登陆这一块肯定是个比较大的难题。
 从今天开始准备一点点对大型网站进行逐个登陆破解。加深自己爬虫水平。

环境搭建

  • Python 3.7.7环境,Mac电脑测试
  • Python内置库
  • 第三方库:rsa、urllib、requests

PC端登陆

全部代码:

'''PC登录哔哩哔哩'''
class Bilibili_For_PC():
  def __init__(self, **kwargs):
    for key, value in kwargs.items(): setattr(self, key, value)
    self.session = requests.Session()
    self.__initialize()
  '''登录函数'''
  def login(self, username, password, crack_captcha_func=None, **kwargs):
    # 若参数中给入代理,则设置
    self.session.proxies.update(kwargs.get('proxies', {}))
    # 是否需要验证码
    is_need_captcha = False
    while True:
      # 需要验证码
      if is_need_captcha:
        captcha_img = self.session.get(self.captcha_url, headers=self.captcha_headers).content
        data = {'image': base64.b64encode(captcha_img).decode('utf-8')}
        captcha = self.session.post(self.crack_captcha_url, json=data).json()['message']
      # 获得key值
      appkey = '1d8b6e7d45233436'
      data = {
            'appkey': appkey,
            'sign': self.__calcSign('appkey={}'.format(appkey))
          }
      response = self.session.post(self.getkey_url, data=data)
      response_json = response.json()
      key_hash = response_json['data']['hash']
      pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(response_json['data']['key'].encode('utf-8'))
      # 模拟登录
      if is_need_captcha:
        data = "access_key=&actionKey=appkey&appkey={}&build=6040500&captcha={}&challenge=&channel=bili&cookies=&device=pc&password={}&permission=ALL&seccode=&subid=1&ts={}&username={}&validate=" \
            .format(appkey, captcha, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt('{}{}'.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username))
      else:
        data = "access_key=&actionKey=appkey&appkey={}&build=6040500&captcha=&challenge=&channel=bili&cookies=&device=pc&password={}&permission=ALL&seccode=&subid=1&ts={}&username={}&validate=" \
            .format(appkey, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt('{}{}'.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username))
      data = "{}&sign={}".format(data, self.__calcSign(data))
      response = self.session.post(self.login_url, data=data, headers=self.login_headers)
      response_json = response.json()
      # 不需要验证码, 登录成功
      if response_json['code'] == 0 and response_json['data']['status'] == 0:
        for cookie in response_json['data']['cookie_info']['cookies']:
          self.session.cookies.set(cookie['name'], cookie['value'], domain='.bilibili')
        print('[INFO]: Account -> %s, login successfully' % username)
        infos_return = {'username': username}
        infos_return.update(response_json)
        return infos_return, self.session
      # 需要识别验证码
      elif response_json['code'] == -105:
        is_need_captcha = True
      # 账号密码错误
      elif response_json['code'] == -629:
        raise RuntimeError('Account -> %s, fail to login, username or password error' % username)
      # 其他错误
      else:
        raise RuntimeError(response_json.get('message'))
  '''计算sign值'''
  def __calcSign(self, param, salt="560c52ccd288fed045859ed18bffd973"):
    sign = hashlib.md5('{}{}'.format(param, salt).encode('utf-8'))
    return sign.hexdigest()
  '''初始化'''
  def __initialize(self):
   # 登陆请求头
    self.login_headers = {'Content-type': 'application/x-www-form-urlencoded'}
    # 破解验证码请求头
    self.captcha_headers = {'Host': 'passport.bilibili.com'}
    # 获取key密钥URL
    self.getkey_url = 'https://passport.bilibili.com/api/oauth2/getKey'
    # 获取登陆URL
    self.login_url = 'https://passport.bilibili.com/api/v3/oauth2/login'
    # 获取验证码URL
    self.captcha_url = 'https://passport.bilibili.com/captcha'
    # 破解网站来自: https://github.com/Hsury/Bilibili-Toolkit
    # 破解验证码URL
    self.crack_captcha_url = 'https://bili.dev:2233/captcha'
    # 请求头都得加这个
    self.session.headers.update({'User-Agent': "Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)"})

移动端登陆

移动端与PC端类似,网址URL差异以及请求头差异。在此不过多介绍。
 全部代码:

'''移动端登录B站'''
class Bilibili_For_Mobile():
  def __init__(self, **kwargs):
    for key, value in kwargs.items(): setattr(self, key, value)
    self.session = requests.Session()
    self.__initialize()
  '''登录函数'''
  def login(self, username, password, crack_captcha_func=None, **kwargs):
    self.session.proxies.update(kwargs.get('proxies', {}))
    # 是否需要验证码
    is_need_captcha = False
    while True:
      # 需要验证码
      if is_need_captcha:
        captcha_img = self.session.get(self.captcha_url, headers=self.captcha_headers).content
        data = {'image': base64.b64encode(captcha_img).decode('utf-8')}
        captcha = self.session.post(self.crack_captcha_url, json=data).json()['message']
      # 获得key值
      appkey = 'bca7e84c2d947ac6'
      data = {
            'appkey': appkey,
            'sign': self.__calcSign('appkey={}'.format(appkey))
          }
      response = self.session.post(self.getkey_url, data=data)
      response_json = response.json()
      key_hash = response_json['data']['hash']
      pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(response_json['data']['key'].encode('utf-8'))
      # 模拟登录
      if is_need_captcha:
        data = "access_key=&actionKey=appkey&appkey={}&build=6040500&captcha={}&challenge=&channel=bili&cookies=&device=phone&mobi_app=android&password={}&permission=ALL&platform=android&seccode=&subid=1&ts={}&username={}&validate=" \
            .format(appkey, captcha, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt('{}{}'.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username))
      else:
        data = "access_key=&actionKey=appkey&appkey={}&build=6040500&captcha=&challenge=&channel=bili&cookies=&device=phone&mobi_app=android&password={}&permission=ALL&platform=android&seccode=&subid=1&ts={}&username={}&validate=" \
            .format(appkey, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt('{}{}'.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username))
      data = "{}&sign={}".format(data, self.__calcSign(data))
      response = self.session.post(self.login_url, data=data, headers=self.login_headers)
      response_json = response.json()
      # 不需要验证码, 登录成功
      if response_json['code'] == 0 and response_json['data']['status'] == 0:
        for cookie in response_json['data']['cookie_info']['cookies']:
          self.session.cookies.set(cookie['name'], cookie['value'], domain='.bilibili')
        print('[INFO]: Account -> %s, login successfully' % username)
        infos_return = {'username': username}
        infos_return.update(response_json)
        return infos_return, self.session
      # 需要识别验证码
      elif response_json['code'] == -105:
        is_need_captcha = True
      # 账号密码错误
      elif response_json['code'] == -629:
        raise RuntimeError('Account -> %s, fail to login, username or password error' % username)
      # 其他错误
      else:
        raise RuntimeError(response_json.get('message'))
  '''计算sign值'''
  def __calcSign(self, param, salt="60698ba2f68e01ce44738920a0ffe768"):
    sign = hashlib.md5('{}{}'.format(param, salt).encode('utf-8'))
    return sign.hexdigest()
  '''初始化'''
  def __initialize(self):
    self.login_headers = {
                'Content-type': 'application/x-www-form-urlencoded'
              }
    self.captcha_headers = {
                'Host': 'passport.bilibili.com'
              }
    self.getkey_url = 'https://passport.bilibili.com/api/oauth2/getKey'
    self.login_url = 'https://passport.bilibili.com/api/v3/oauth2/login'
    self.captcha_url = 'https://passport.bilibili.com/captcha'
    # 破解网站来自: https://github.com/Hsury/Bilibili-Toolkit
    self.crack_captcha_url = 'https://bili.dev:2233/captcha'
    self.session.headers.update({'User-Agent': "Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)"})

到此这篇关于Python爬虫破解登陆哔哩哔哩的方法的文章就介绍到这了,更多相关Python爬虫破解登陆内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中__call__用法实例
Aug 29 Python
Python实现单词拼写检查
Apr 25 Python
python利用Guetzli批量压缩图片
Mar 23 Python
python学习之matplotlib绘制散点图实例
Dec 09 Python
scikit-learn线性回归,多元回归,多项式回归的实现
Aug 29 Python
pytorch 中pad函数toch.nn.functional.pad()的用法
Jan 08 Python
python获取响应某个字段值的3种实现方法
Apr 30 Python
python函数中将变量名转换成字符串实例
May 11 Python
python3.x中安装web.py步骤方法
Jun 23 Python
python如何控制进程或者线程的个数
Oct 16 Python
python中not、and和or的优先级与详细用法介绍
Nov 03 Python
在NumPy中深拷贝和浅拷贝相关操作的定义和背后的原理
Apr 14 Python
appium+python自动化配置(adk、jdk、node.js)
Nov 17 #Python
python调用百度API实现人脸识别
Nov 17 #Python
详解利用python识别图片中的条码(pyzbar)及条码图片矫正和增强
Nov 17 #Python
详解Pytorch显存动态分配规律探索
Nov 17 #Python
Python调用ffmpeg开源视频处理库,批量处理视频
Nov 16 #Python
python tkinter实现连连看游戏
Nov 16 #Python
详解python os.path.exists判断文件或文件夹是否存在
Nov 16 #Python
You might like
如何给phpcms v9增加类似于phpcms 2008中的关键词表
2013/07/01 PHP
通过table标签,PHP输出EXCEL的实现方法
2013/07/24 PHP
PHP连接MSSQL方法汇总
2016/02/05 PHP
php生成Android客户端扫描可登录的二维码
2016/05/13 PHP
PC端微信扫码支付成功之后自动跳转php版代码
2017/07/07 PHP
php识别翻转iphone拍摄的颠倒图片
2018/05/17 PHP
jquery 关于event.target使用的几点说明介绍
2013/04/26 Javascript
Jquery搜索父元素操作方法
2015/02/10 Javascript
Easyui form combobox省市区三级联动
2016/01/13 Javascript
jQuery html表格排序插件tablesorter使用方法详解
2017/02/10 Javascript
使用JS 插件qrcode.js生成二维码功能
2017/02/20 Javascript
在javaScript中检测数据类型的几种方式小结
2017/03/04 Javascript
jquery无缝图片轮播组件封装
2020/11/25 jQuery
生成无限制的微信小程序码的示例代码
2019/09/20 Javascript
微信小程序实现首页弹出广告
2020/12/03 Javascript
vue 使用class创建和清除水印的示例代码
2020/12/25 Vue.js
[30:37]【全国守擂赛】第三周擂主赛 Dark Knight vs. Leopard Gaming
2020/05/04 DOTA
python中 ? : 三元表达式的使用介绍
2013/10/09 Python
python字符串对其居中显示的方法
2015/07/11 Python
Python 自动化表单提交实例代码
2017/06/08 Python
python3+PyQt5+Qt Designer实现扩展对话框
2018/04/20 Python
Python编程flask使用页面模版的方法
2018/12/28 Python
python3正则提取字符串里的中文实例
2019/01/31 Python
python flask框架实现传数据到js的方法分析
2019/06/11 Python
Cython编译python为so 代码加密示例
2019/12/23 Python
python实现用类读取文件数据并计算矩形面积
2020/01/18 Python
利用python对excel中一列的时间数据更改格式操作
2020/07/14 Python
python 通过pip freeze、dowload打离线包及自动安装的过程详解(适用于保密的离线环境
2020/12/14 Python
波兰补充商店:Muscle Power
2018/10/29 全球购物
SQL里面如何插入自动增长序列号字段
2012/03/29 面试题
甲方资料员岗位职责
2013/12/13 职场文书
联谊活动策划书
2014/01/26 职场文书
大学活动总结范文
2014/04/29 职场文书
节约用电通知
2015/04/25 职场文书
单位接收证明格式
2015/06/18 职场文书
phpQuery解析HTML乱码问题(补充官网未列出的乱码解决方案)
2021/04/01 PHP