<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
B站的關注列表在
https://api.bilibili.com/x/relation/followings?vmid=UID&pn=1&ps=50&order=desc&order_type=attention
中,一頁最多50條資訊。
我們大致分析一下資訊,
{ "code": 0, "message": "0", "ttl": 1, "data": { "list": [{……
首先,列表內容存在data:list裡。
其次,對於列表中每一項,有如下資訊
"mid": 672353429, "attribute": 2, "mtime": 1630510107, "tag": null, "special": 0, "contract_info": { "is_contractor": false, "ts": 0, "is_contract": false, "user_attr": 0 }, "uname": "貝拉kira", "face": "http://i2.hdslb.com/bfs/face/668af440f8a8065743d3fa79cfa8f017905d0065.jpg", "sign": "元氣滿滿的A-SOUL舞擔參上~目標TOP IDOL,一起加油!", "official_verify": { "type": 0, "desc": "虛擬偶像團體A-SOUL 所屬藝人" }, "vip": { "vipType": 2, "vipDueDate": 1674576000000, "dueRemark": "", "accessStatus": 0, "vipStatus": 1, "vipStatusWarn": "", "themeType": 0, "label": { "path": "", "text": "年度大會員", "label_theme": "annual_vip", "text_color": "#FFFFFF", "bg_style": 1, "bg_color": "#FB7299", "border_color": "" }, "avatar_subscript": 1, "nickname_color": "#FB7299", "avatar_subscript_url": "http://i0.hdslb.com/bfs/vip/icon_Certification_big_member_22_3x.png" }
其中,mid為使用者獨一無二的UID,vipType,0是什麼都沒開,1是大會員,2是年度大會員,official_verify中,type 0代表官方認證,-1代表沒有官方認證。
同時我們發現,如果對方鎖了列表,會返回
{"code":-400,"message":"請求錯誤","ttl":1}
基於這些,我們先設計資料庫,包含兩張表,使用者資訊的基本屬性表和關注的關係表。
def createDB(): link=sqlite3.connect('BiliFollowDB.db') print("database open success") UserTableDDL=''' create table if not exists user( UID int PRIMARY KEY NOT NULL, NAME varchar NOT NULL, SIGN varchar DEFAULT NULL, vipType int NOT NULL, verifyType int NOT NULL, verifyDesc varchar DEFAULT NULL) ''' RelationTableDDL=''' create table if not exists relation( follower int NOT NULL, following int NOT NULL, followTime int NOT NULL, PRIMARY KEY (follower,following), FOREIGN KEY(follower,following) REFERENCES user(UID,UID) ) ''' # create user table link.execute(UserTableDDL) # create relation table link.execute(RelationTableDDL) print("database create success") link.commit() link.close()
其次是插入新使用者的列表,我的思路是爬完一個人的關注列表,把一整個list丟給該函數,判斷是否存在新增使用者,存在則把新增使用者傳回,作為下一次爬蟲的起點。
def insertUser(infos): conn=sqlite3.connect('BiliFollowDB.db') link=conn.cursor() InsertCmd="insert into user (UID,NAME,vipType,verifyType,sign,verifyDesc) values (?,?,?,?,?,?);" ExistCmd="select count(UID) from user where UID='%d';"# % UID newID=[] for info in infos: answer=link.execute(ExistCmd%info['uid']) for row in answer: exist_ID=row[0] if exist_ID==0: newID.append(info['uid']) link.execute(InsertCmd,(info['uid'],info['name'],info['vipType'],info['verifyType'],info['sign'],info['verifyDesc'])) conn.commit() conn.close() return newID
然後是插入關係的函數,這個比較簡單
def insertFollowing(uid:int,subscribe): conn=sqlite3.connect('BiliFollowDB.db') link=conn.cursor() InsertCmd="insert into relation (follower,following,followTime) values (?,?,?);" for follow in subscribe: link.execute(InsertCmd,(uid,follow[0],follow[1])) conn.commit() conn.close()
通過觀察,我們發現睿叔叔鎖了5頁的關注列表
即使是人工操作也只能存取5頁,那沒辦法啦,我們就爬5頁吧。
def getFollowingList(uid:int): url="https://api.bilibili.com/x/relation/followings?vmid=%d&pn=%d&ps=50&order=desc&order_type=attention&jsonp=jsonp"# % (UID, Page Number) infos=[] subscribe=[] for i in range(1,6): html=requests.get(url%(uid,i)) if html.status_code!=200: print("GET ERROR!") text=html.text dic=json.loads(text) if dic['code']==-400: break list=dic['data']['list'] for usr in list: info={} info['uid']=usr['mid'] info['name']=usr['uname'] info['vipType']=usr['vip']['vipType'] info['verifyType']=usr['official_verify']['type'] info['sign']=usr['sign'] if info['verifyType']==-1: info['verifyDesc']='NULL' else : info['verifyDesc']=usr['official_verify']['desc'] subscribe.append((usr['mid'],usr['mtime'])) infos.append(info) newID=insertUser(infos) insertFollowing(uid,subscribe) return newID
#by concyclics # -*- coding:UTF-8 -*- import sqlite3 import json import requests def createDB(): link=sqlite3.connect('BiliFollowDB.db') print("database open success") UserTableDDL=''' create table if not exists user( UID int PRIMARY KEY NOT NULL, NAME varchar NOT NULL, SIGN varchar DEFAULT NULL, vipType int NOT NULL, verifyType int NOT NULL, verifyDesc varchar DEFAULT NULL) ''' RelationTableDDL=''' create table if not exists relation( follower int NOT NULL, following int NOT NULL, followTime int NOT NULL, PRIMARY KEY (follower,following), FOREIGN KEY(follower,following) REFERENCES user(UID,UID) ) ''' # create user table link.execute(UserTableDDL) # create relation table link.execute(RelationTableDDL) print("database create success") link.commit() link.close() def insertUser(infos): conn=sqlite3.connect('BiliFollowDB.db') link=conn.cursor() InsertCmd="insert into user (UID,NAME,vipType,verifyType,sign,verifyDesc) values (?,?,?,?,?,?);" ExistCmd="select count(UID) from user where UID='%d';"# % UID newID=[] for info in infos: answer=link.execute(ExistCmd%info['uid']) for row in answer: exist_ID=row[0] if exist_ID==0: newID.append(info['uid']) link.execute(InsertCmd,(info['uid'],info['name'],info['vipType'],info['verifyType'],info['sign'],info['verifyDesc'])) conn.commit() conn.close() return newID def insertFollowing(uid:int,subscribe): conn=sqlite3.connect('BiliFollowDB.db') link=conn.cursor() InsertCmd="insert into relation (follower,following,followTime) values (?,?,?);" for follow in subscribe: try: link.execute(InsertCmd,(uid,follow[0],follow[1])) except: print((uid,follow[0],follow[1])) conn.commit() conn.close() def getFollowingList(uid:int): url="https://api.bilibili.com/x/relation/followings?vmid=%d&pn=%d&ps=50&order=desc&order_type=attention&jsonp=jsonp"# % (UID, Page Number) infos=[] subscribe=[] for i in range(1,6): html=requests.get(url%(uid,i)) if html.status_code!=200: print("GET ERROR!") return [] text=html.text dic=json.loads(text) if dic['code']==-400: return [] try: list=dic['data']['list'] except: return [] for usr in list: info={} info['uid']=usr['mid'] info['name']=usr['uname'] info['vipType']=usr['vip']['vipType'] info['verifyType']=usr['official_verify']['type'] info['sign']=usr['sign'] if info['verifyType']==-1: info['verifyDesc']='NULL' else : info['verifyDesc']=usr['official_verify']['desc'] subscribe.append((usr['mid'],usr['mtime'])) infos.append(info) newID=insertUser(infos) insertFollowing(uid,subscribe) return newID def getFollowingUid(uid:int): url="https://api.bilibili.com/x/relation/followings?vmid=%d&pn=%d&ps=50&order=desc&order_type=attention&jsonp=jsonp"# % (UID, Page Number) for i in range(1,6): html=requests.get(url%(uid,i)) if html.status_code!=200: print("GET ERROR!") return [] text=html.text dic=json.loads(text) if dic['code']==-400: return [] try: list=dic['data']['list'] except: return [] IDs=[] for usr in list: IDs.append(usr['mid']) return IDs def work(root): IDlist=root tmplist=[] while len(IDlist)!=0: tmplist=[] for ID in IDlist: print(ID) tmplist+=getFollowingList(ID) IDlist=tmplist def rework(): conn=sqlite3.connect('BiliFollowDB.db') link=conn.cursor() SelectCmd="select uid from user;" answer=link.execute(SelectCmd) IDs=[] for row in answer: IDs.append(row[0]) conn.commit() conn.close() newID=[] print(IDs) for ID in IDs: ids=getFollowingUid(ID) for id in ids: if id not in IDs: newID.append(id) return newID if __name__=="__main__": createDB() #work([**put root UID here**,])
https://github.com/Concyclics/BiliBiliFollowSpider
以上就是python爬取B站關注列表及資料庫的設計與操作的詳細內容,更多關於python爬取B站關注列表的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45