生成需要参数的网址:https://passport.bilibili.com/qrcode/getLoginUrl
生成两个重要参数:url、oauthKey
session = requests.session()
getlogin = session.get('https://passport.bilibili.com/qrcode/getLoginUrl', headers=headers).json()
oauthKey = getlogin['data']['oauthKey']
#输出getlogin = {'code': 0, 'status': True, 'ts': 16……, 'data': {'url': 'https://passpor^', 'oauthKey': 'c4a003^'}}
getQrcode: function() {
var e = this;
$.ajax({
url: "https://passport.bilibili.com/qrcode/getLoginUrl",
dataType: "json"
}).done(function(t) {
if (t.status) {
e.refer ? (e.refer.clear(),
e.refer.makeCode(t.data.url)) : e.$nextTick(function() {
e.refer = new QRCode(e.$refs.qrcode,{
text: t.data.url,
width: 140,
height: 140
})
}),
e.key = t.data.oauthKey;
var i = decodeURIComponent(Object(f.h)("gourl"));
clearTimeout(e.cd),
e.cd = setTimeout(e.expire, e.cdTime),
clearInterval(e.loop),
e.loop = setInterval(function() {
$.ajax({
url: "https://passport.bilibili.com/qrcode/getLoginInfo",
dataType: "json",
type: "POST",
data: {
oauthKey: e.key,
gourl: i || (document.referrer || "")
}
}).done(function(t) {
t.status ? (reportMsgObj.qrcodescan_login = "success",
window.reportObserver && window.reportObserver.forceCommit && window.reportObserver.forceCommit(),
window.location.href = t.data.url) : t.status || -2 != t.message ? t.status || -5 != t.data || e.scanSucess() : e.expire()
})
}, e.loopTime)
}
})
},
getlogin = session.get('https://passport.bilibili.com/qrcode/getLoginUrl', headers=headers).json()
loginurl = requests.get(getlogin['data']['url'], headers=headers).url
#输出loginurl = https://passport.bilibili.com/mobile/h5-confirm……
class showpng(Thread):
def __init__(self, data):
Thread.__init__(self)
self.data = data
def run(self):
img = Image.open(BytesIO(self.data))
img.show()
loginurl = requests.get(getlogin['data']['url'], headers=headers).url
#生成二维码并进行缓存,
qr = qrcode.QRCode()
qr.add_data(loginurl)
img = qr.make_image()
#缓存的好处就是不需要保存本地
a = BytesIO()
img.save(a, 'png')
png = a.getvalue()
a.close()
#打开二维码进行扫码操作
t = showpng(png)
t.start()
tokenurl = 'https://passport.bilibili.com/qrcode/getLoginInfo'
#将oauthKey参数带入其中进行访问,获得登录状态信息
qrcodedata = session.post(tokenurl, data={
'oauthKey': oauthKey, 'gourl': 'https://www.bilibili.com/'}, headers=headerss).json()
#输出 qrcodedata = {'status': False, 'data': -4, 'message': "Can't scan~"}
‘data’: -4:二维码未失效
‘data’: -5:已扫码
‘data’: -2:二维码已失效
‘status’: True:已登录
while 1:
qrcodedata = session.post(tokenurl, data={
'oauthKey':oauthKey,'gourl':'https://www.bilibili.com/'}, headers=headerss).json()
if '-4' in str(qrcodedata['data']): #'-4' in qrcodedata['data']:
print('二维码未失效,请扫码!')
elif '-5' in str(qrcodedata['data']):
print('已扫码,请确认!')
elif '-2' in str(qrcodedata['data']):
print('二维码已失效,请重新运行!')
elif 'True' in str(qrcodedata['status']):
print('已确认,登入成功!')
#最后一步生成的url参数,进行访问获取最终真正的cookie值
session.get(qrcodedata['data']['url'], headers=headers)
#登录后停止运行break
break
else:
print('其他:',qrcodedata)
time.sleep(2)
loginurl = session.get("https://api.bilibili.com/x/web-interface/nav", verify=False, headers=headers).json()
没登陆生成的参数:
{‘code’: -101, ‘message’: ‘账号未登录’, ‘ttl’: 1, ‘data’: {‘isLogin’: False}}
登录后生成的参数:
{‘code’: 0, ‘message’: ‘0’, ‘ttl’: 1, ‘data’: {‘isLogin’: Tr……}
loginurl = session.get("https://api.bilibili.com/x/web-interface/nav", verify=False, headers=headers).json()
if loginurl['code'] == 0:
print('Cookies值有效,',loginurl['data']['uname'],',已登录!')
return session, True
else:
print('Cookies值已经失效,请重新扫码登录!')
return session, False
# -*- coding: utf-8 -*-
import qrcode
import agent
from threading import Thread
import time
import requests
from io import BytesIO
import http.cookiejar as cookielib
from PIL import Image
import os
requests.packages.urllib3.disable_warnings()
headers = {
'User-Agent': agent.get_user_agents(), 'Referer': "https://www.bilibili.com/"}
headerss = {
'User-Agent': agent.get_user_agents(), 'Host': 'passport.bilibili.com','Referer': "https://passport.bilibili.com/login"}
class showpng(Thread):
def __init__(self, data):
Thread.__init__(self)
self.data = data
def run(self):
img = Image.open(BytesIO(self.data))
img.show()
def islogin(session):
try:
session.cookies.load(ignore_discard=True)
except Exception:
pass
loginurl = session.get("https://api.bilibili.com/x/web-interface/nav", verify=False, headers=headers).json()
if loginurl['code'] == 0:
print('Cookies值有效,',loginurl['data']['uname'],',已登录!')
return session, True
else:
print('Cookies值已经失效,请重新扫码登录!')
return session, False
def bzlogin():
if not os.path.exists('bzcookies.txt'):
with open("bzcookies.txt", 'w') as f:
f.write("")
session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename='bzcookies.txt')
session, status = islogin(session)
if not status:
getlogin = session.get('https://passport.bilibili.com/qrcode/getLoginUrl', headers=headers).json()
loginurl = requests.get(getlogin['data']['url'], headers=headers).url
oauthKey = getlogin['data']['oauthKey']
qr = qrcode.QRCode()
qr.add_data(loginurl)
img = qr.make_image()
a = BytesIO()
img.save(a, 'png')
png = a.getvalue()
a.close()
t = showpng(png)
t.start()
tokenurl = 'https://passport.bilibili.com/qrcode/getLoginInfo'
while 1:
qrcodedata = session.post(tokenurl, data={
'oauthKey': oauthKey, 'gourl': 'https://www.bilibili.com/'}, headers=headerss).json()
print(qrcodedata)
if '-4' in str(qrcodedata['data']):
print('二维码未失效,请扫码!')
elif '-5' in str(qrcodedata['data']):
print('已扫码,请确认!')
elif '-2' in str(qrcodedata['data']):
print('二维码已失效,请重新运行!')
elif 'True' in str(qrcodedata['status']):
print('已确认,登入成功!')
session.get(qrcodedata['data']['url'], headers=headers)
break
else:
print('其他:', qrcodedata)
time.sleep(2)
session.cookies.save()
return session
if __name__ == '__main__':
bzlogin()
- 后期小编将开设登录后批量采集各平台数据(点赞、播放量、评论、图片、视频、音乐等)专栏文章!记得关注哟!
- 如果文章能帮到您,愿意给小编点个 赞 吗,么么哒~ (●’◡’●)