最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何使用python爬取B站排行榜Top100的視頻數(shù)據(jù)

 更新時間:2021年09月27日 09:59:41   作者:小狐貍夢想去童話鎮(zhèn)  
本文章向大家介紹python爬取b站排行榜,包括python爬取b站排行榜的具體代碼,對大家的學(xué)習或工作具有一定的參考價值,需要的朋友可以參考一下

記得收藏呀?。?!

1、第三方庫導(dǎo)入

from bs4 import BeautifulSoup # 解析網(wǎng)頁
import re   # 正則表達式,進行文字匹配
import urllib.request,urllib.error  # 通過瀏覽器請求數(shù)據(jù)
import sqlite3  # 輕型數(shù)據(jù)庫
import time  # 獲取當前時間

2、程序運行主函數(shù)

爬取過程主要包括聲明爬取網(wǎng)頁 -> 爬取網(wǎng)頁數(shù)據(jù)并解析 -> 保存數(shù)據(jù)

def main():
	#聲明爬取網(wǎng)站
    baseurl = "https://www.bilibili.com/v/popular/rank/all"
    #爬取網(wǎng)頁
    datalist = getData(baseurl)
    # print(datalist)
    #保存數(shù)據(jù)
    dbname = time.strftime("%Y-%m-%d", time.localtime())
    dbpath = "BiliBiliTop100  " + dbname
    saveData(datalist,dbpath)

(1)在爬取的過程中采用的技術(shù)為:偽裝成瀏覽器對數(shù)據(jù)進行請求;
(2)解析爬取到的網(wǎng)頁源碼時:采用Beautifulsoup解析出需要的數(shù)據(jù),使用re正則表達式對數(shù)據(jù)進行匹配;
(3)保存數(shù)據(jù)時,考慮到B站排行榜是每日進行刷新,故可以用當前日期進行保存數(shù)據(jù)庫命名。

3、程序運行結(jié)果

在這里插入圖片描述

數(shù)據(jù)庫中包含的數(shù)據(jù)有:排名、視頻鏈接、標題、播放量、評論量、作者、綜合分數(shù)這7個數(shù)據(jù)。

在這里插入圖片描述

4、程序源代碼

from bs4 import BeautifulSoup #解析網(wǎng)頁
import re # 正則表達式,進行文字匹配
import urllib.request,urllib.error
import sqlite3
import time


def main():
    #聲明爬取網(wǎng)站
    baseurl = "https://www.bilibili.com/v/popular/rank/all"
    #爬取網(wǎng)頁
    datalist = getData(baseurl)
    # print(datalist)
    #保存數(shù)據(jù)
    dbname = time.strftime("%Y-%m-%d", time.localtime())
    dbpath = "BiliBiliTop100  " + dbname
    saveData(datalist,dbpath)

#re正則表達式
findLink =re.compile(r'<a class="title" href="(.*?)" rel="external nofollow" ') #視頻鏈接
findOrder = re.compile(r'<div class="num">(.*?)</div>') #榜單次序
findTitle = re.compile(r'<a class="title" href=".*?" rel="external nofollow"  rel="external nofollow"  target="_blank">(.*?)</a>') #視頻標題
findPlay = re.compile(r'<span class="data-box"><i class="b-icon play"></i>([\s\S]*)(.*?)</span> <span class="data-box">') #視頻播放量
findView = re.compile(r'<span class="data-box"><i class="b-icon view"></i>([\s\S]*)(.*?)</span> <a href=".*?" rel="external nofollow"  rel="external nofollow"  target="_blank"><span class="data-box up-name">') # 視頻評價數(shù)
findName = re.compile(r'<i class="b-icon author"></i>(.*?)</span></a>',re.S) #視頻作者
findScore = re.compile(r'<div class="pts"><div>(.*?)</div>綜合得分',re.S) #視頻得分
def getData(baseurl):
    datalist = []
    html = askURL(baseurl)
    #print(html)

    soup = BeautifulSoup(html,'html.parser')  #解釋器
    for item in soup.find_all('li',class_="rank-item"):
        # print(item)
        data = []
        item = str(item)

        Order = re.findall(findOrder,item)[0]
        data.append(Order)
        # print(Order)

        Link = re.findall(findLink,item)[0]
        Link = 'https:' + Link
        data.append(Link)
        # print(Link)

        Title = re.findall(findTitle,item)[0]
        data.append(Title)
        # print(Title)

        Play = re.findall(findPlay,item)[0][0]
        Play = Play.replace(" ","")
        Play = Play.replace("\n","")
        Play = Play.replace(".","")
        Play = Play.replace("萬","0000")
        data.append(Play)
        # print(Play)

        View = re.findall(findView,item)[0][0]
        View = View.replace(" ","")
        View = View.replace("\n","")
        View = View.replace(".","")
        View = View.replace("萬","0000")
        data.append(View)
        # print(View)

        Name = re.findall(findName,item)[0]
        Name = Name.replace(" ","")
        Name = Name.replace("\n","")
        data.append(Name)
        # print(Name)

        Score = re.findall(findScore,item)[0]
        data.append(Score)
        # print(Score)
        datalist.append(data)
    return datalist

def askURL(url):
    #設(shè)置請求頭
    head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0;Win64;x64) AppleWebKit/537.36(KHTML, likeGecko) Chrome/80.0.3987.163Safari/537.36"
    }
    request = urllib.request.Request(url, headers = head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
        #print(html)
    except urllib.error.URLError as e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print(e.reason)
    return html

def saveData(datalist,dbpath):
    init_db(dbpath)
    conn = sqlite3.connect(dbpath)
    cur = conn.cursor()

    for data in datalist:
        sql = '''
        insert into Top100(
        id,info_link,title,play,view,name,score)
        values("%s","%s","%s","%s","%s","%s","%s")'''%(data[0],data[1],data[2],data[3],data[4],data[5],data[6])
        print(sql)
        cur.execute(sql)
        conn.commit()
    cur.close()
    conn.close()

def init_db(dbpath):
    sql = '''
    create table Top100
    (
    id integer primary key autoincrement,
    info_link text,
    title text,
    play numeric,
    view numeric,
    name text,
    score numeric
    )
    '''
    conn = sqlite3.connect(dbpath)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()
    conn.close()



if __name__ =="__main__":
    main()

到此這篇關(guān)于如何使用python爬取B站排行榜Top100的視頻數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python B站視頻 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 深入解析opencv骨架提取的算法步驟

    深入解析opencv骨架提取的算法步驟

    這篇文章主要介紹了深入解析opencv骨架提取的算法步驟
    2022-05-05
  • python非對稱加密算法RSA實現(xiàn)原理與應(yīng)用詳解

    python非對稱加密算法RSA實現(xiàn)原理與應(yīng)用詳解

    RSA加密算法是一種非對稱加密算法,RSA算法的安全性基于大數(shù)分解的困難性,即已知兩個大素數(shù)p和q的乘積n,求解p和q非常困難,RSA算法廣泛應(yīng)用于數(shù)據(jù)加密和數(shù)字簽名等領(lǐng)域,本文將詳細介紹如何在Python中使用RSA算法進行加密和解密,需要的朋友可以參考下
    2024-09-09
  • python協(xié)程庫asyncio(異步io)問題

    python協(xié)程庫asyncio(異步io)問題

    這篇文章主要介紹了python協(xié)程庫asyncio(異步io)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Python構(gòu)造函數(shù)屬性示例魔法解析

    Python構(gòu)造函數(shù)屬性示例魔法解析

    Python構(gòu)造函數(shù)和屬性魔法是面向?qū)ο缶幊讨械年P(guān)鍵概念,它們允許在類定義中執(zhí)行特定操作,以控制對象的初始化和屬性訪問,本文將深入學(xué)習Python中的構(gòu)造函數(shù)和屬性魔法,包括構(gòu)造函數(shù)__init__、屬性的@property和@attribute.setter等,以及它們的實際應(yīng)用
    2023-12-12
  • Python讀寫/追加excel文件Demo分享

    Python讀寫/追加excel文件Demo分享

    今天小編就為大家分享一篇Python讀寫/追加excel文件Demo,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 解決安裝和導(dǎo)入tensorflow、keras出錯的問題

    解決安裝和導(dǎo)入tensorflow、keras出錯的問題

    這篇文章主要介紹了解決安裝和導(dǎo)入tensorflow、keras出錯的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Python實現(xiàn)的Google IP 可用性檢測腳本

    Python實現(xiàn)的Google IP 可用性檢測腳本

    這篇文章主要介紹了Python實現(xiàn)的Google IP 可用性檢測腳本,本文腳本需要Python 3.4+環(huán)境,需要的朋友可以參考下
    2015-04-04
  • Tensorflow分類器項目自定義數(shù)據(jù)讀入的實現(xiàn)

    Tensorflow分類器項目自定義數(shù)據(jù)讀入的實現(xiàn)

    這篇文章主要介紹了Tensorflow分類器項目自定義數(shù)據(jù)讀入的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • Python程序退出方式小結(jié)

    Python程序退出方式小結(jié)

    這篇文章主要介紹了Python程序退出方式小結(jié),具有一定參考價值,需要的朋友可以了解下。
    2017-12-12
  • 對python中GUI,Label和Button的實例詳解

    對python中GUI,Label和Button的實例詳解

    今天小編就為大家分享一篇對python中GUI,Label和Button的實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06

最新評論

师宗县| 翼城县| 巴青县| 邢台县| 新密市| 博野县| 军事| 临洮县| 阿克| 徐闻县| 郎溪县| 大同市| 金堂县| 永城市| 盐津县| 海安县| 澄迈县| 辽宁省| 饶阳县| 光泽县| 汉川市| 玛曲县| 屯门区| 海淀区| 乐亭县| 河间市| 五常市| 武强县| 曲阳县| 达日县| 雷山县| 贞丰县| 息烽县| 广饶县| 上饶县| 宁安市| 商都县| 拉萨市| 胶南市| 托克逊县| 贵南县|