欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬取B站關注列表及數(shù)據(jù)庫的設計與操作

 更新時間:2022年05月18日 15:24:59   作者:concyclics  
這篇文章主要為大家介紹了python爬取B站關注列表及數(shù)據(jù)庫的設計與操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

一、數(shù)據(jù)庫的設計與操作

1、數(shù)據(jù)的分析

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代表沒有官方認證。

同時我們發(fā)現(xiàn),如果對方鎖了列表,會返回

{"code":-400,"message":"請求錯誤","ttl":1}

2、數(shù)據(jù)庫設計

基于這些,我們先設計數(shù)據(jù)庫,包含兩張表,用戶信息的基本屬性表和關注的關系表。

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()

3、數(shù)據(jù)庫操作

其次是插入新用戶的列表,我的思路是爬完一個人的關注列表,把一整個list丟給該函數(shù),判斷是否存在新增用戶,存在則把新增用戶傳回,作為下一次爬蟲的起點。

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

然后是插入關系的函數(shù),這個比較簡單

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()
 

二、爬蟲

通過觀察,我們發(fā)現(xiàn)睿叔叔鎖了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站關注列表及數(shù)據(jù)庫的設計與操作的詳細內容,更多關于python爬取B站關注列表的資料請關注腳本之家其它相關文章!

相關文章

  • python整小時 整天時間戳獲取算法示例

    python整小時 整天時間戳獲取算法示例

    今天小編就為大家分享一篇python整小時 整天時間戳獲取算法示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • 最新Python idle下載、安裝與使用教程圖文詳解

    最新Python idle下載、安裝與使用教程圖文詳解

    這篇文章主要介紹了最新Python idle下載、安裝與使用教程圖文詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • python實現(xiàn)在函數(shù)中修改變量值的方法

    python實現(xiàn)在函數(shù)中修改變量值的方法

    今天小編就為大家分享一篇python實現(xiàn)在函數(shù)中修改變量值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python實現(xiàn)網站微信登錄的示例代碼

    python實現(xiàn)網站微信登錄的示例代碼

    這篇文章主要介紹了python實現(xiàn)網站微信登錄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Python項目打包成exe文件

    Python項目打包成exe文件

    大家好,本篇文章主要講的是Python項目打包成exe文件,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Python設計模式之橋接模式原理與用法實例分析

    Python設計模式之橋接模式原理與用法實例分析

    這篇文章主要介紹了Python設計模式之橋接模式原理與用法,結合具體實例形式分析了Python橋接模式的相關概念、原理、定義及使用方法,需要的朋友可以參考下
    2019-01-01
  • 利用Python讀取Excel表內容的詳細過程

    利用Python讀取Excel表內容的詳細過程

    python有多種方式可以去讀取excel文檔的內容,下面這篇文章主要給大家介紹了利用Python讀取Excel表內容的詳細過程,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-10-10
  • python微信公眾號之關鍵詞自動回復

    python微信公眾號之關鍵詞自動回復

    這篇文章主要為大家詳細介紹了python微信公眾號之關鍵詞自動回復,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Python ldap實現(xiàn)登錄實例代碼

    Python ldap實現(xiàn)登錄實例代碼

    今天給大家分享python idap實現(xiàn)登錄的實例代碼,代碼簡單易懂,需要的朋友一起看看吧
    2016-09-09
  • Python Flask 請求數(shù)據(jù)獲取響應詳解

    Python Flask 請求數(shù)據(jù)獲取響應詳解

    這篇文章主要介紹了Python Flask請求數(shù)據(jù)獲取響應的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-10-10

最新評論