Python模拟登陆 —— 征服验证码 2 B站

B站的登录密码用了rsa加密(两个大质数的乘积很难进行逆向分解,所以可以用这个乘积来做公钥)。
所以运行py文件之前,使用镜像,先用pip安装rsa库:

pip install rsa -i https://pypi.tuna.tsinghua.edu.cn/simple/

运行:

import requests
import re
import time
import sys
import json  
import rsa
import os.path
import binascii
import datetime
from bs4 import BeautifulSoup
try:
    import cookielib
except:
    import http.cookiejar as cookielib

try:
    from PIL import Image
except:
    pass
    
session = requests.Session()
session.headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}

# 使用登录cookie信息
session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename='cookies')
try:
    session.cookies.load(ignore_discard=True)
except:
    print("Cookie 未能加载")

def rsaEncrypt(password):
    url = 'http://passport.bilibili.com/login?act=getkey'
    try:
        getKeyRes = session.get(url)
        token = json.loads(getKeyRes.content.decode('utf-8'))
        pw = str(token['hash'] + password).encode('utf-8')

        key = token['key']
        key = rsa.PublicKey.load_pkcs1_openssl_pem(key)

        pw = rsa.encrypt(pw, key)
        password = binascii.b2a_base64(pw)
        return password
    except:
        return False

def get_vdcode():
    t = str(int(time.time()*1000))
    captcha_url = 'https://passport.bilibili.com/captcha.gif?r=' + t + "&type=login"
    r = session.get(captcha_url)
    with open('captcha.jpg', 'wb') as f:
        f.write(r.content)
        f.close()
    # 用pillow 的 Image 显示验证码
    # 如果没有安装 pillow 到源代码所在的目录去找到验证码然后手动输入
    try:
        im = Image.open('captcha.jpg')
        im.show()
        im.close()
    except:
        print(u'请到 %s 目录找到captcha.jpg 手动输入' % os.path.abspath('captcha.jpg'))
    captcha = input("please input the captcha\n>")
    return captcha
    
def login(user, password):
    post_url = 'https://passport.bilibili.com/login/dologin'
    payload = {
        'act': 'login',
        'gourl': '',
        'keeptime': '2592000',
        'userid': user,
        'pwd': password,
        'vdcode': get_vdcode(),
    }
    if payload["vdcode"] == None:
        return False

    try:
        resp = session.post(post_url, data=payload)
        session.cookies.save()

        soup = BeautifulSoup(resp.content, 'lxml')
        s = str(soup.select('center')[0])
        s = s.replace('\n', '')
        s = s.replace('\r', '')
        s = s.replace(' ', '')
        s = s.split('>')
        s = s[2]
        s = s.replace('<br/', '')
        flash(s)
        return False
    except requests.exceptions.ConnectionError as e:
        flash(e)
        return False
    except:
        return True

def isLogin():
    url = 'https://account.bilibili.com/home/userInfo'
    resp = session.get(url, allow_redirects=False)
    if  resp.status_code == 200 and resp.json()['code'] == 0:
        return True
    else:
        return False

if __name__ == '__main__':
    if isLogin():
        print('您已经登录')
    else:
        account = input('请输入你的用户名:')
        password = input('请输入密码:')
        login(account, rsaEncrypt(password))

滑块验证见http://www.jianshu.com/p/7623ff64ee54

img_d4b83a1a114a9e45d25ece176723087e.png
登录界面

img_640c74f4bb49089767bbc3aec946b7ab.jpe
一张有趣的合照
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值