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

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

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

記得收藏呀!??!

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

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

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

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

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

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

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

在這里插入圖片描述

數(shù)據(jù)庫(kù)中包含的數(shù)據(jù)有:排名、視頻鏈接、標(biāo)題、播放量、評(píng)論量、作者、綜合分?jǐn)?shù)這7個(gè)數(shù)據(jù)。

在這里插入圖片描述

4、程序源代碼

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


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

#re正則表達(dá)式
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>') #視頻標(biāo)題
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">') # 視頻評(píng)價(jià)數(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("萬(wàn)","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("萬(wàn)","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è)置請(qǐng)求頭
    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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python調(diào)整PDF文檔頁(yè)邊距的方法小結(jié)

    Python調(diào)整PDF文檔頁(yè)邊距的方法小結(jié)

    PDF 文檔中的邊距是指環(huán)繞每頁(yè)內(nèi)容的空白區(qū)域,充當(dāng)文本或圖像與頁(yè)面邊緣之間的緩沖區(qū),本文將介紹如何使用 Spire.PDF for Python 修改 PDF 文檔的頁(yè)邊距,為不同使用場(chǎng)景定制合適的文檔布局,需要的朋友可以參考下
    2024-05-05
  • python批量修改文件編碼格式的方法

    python批量修改文件編碼格式的方法

    這篇文章主要為大家詳細(xì)介紹了python批量修改文件編碼格式的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 在tensorflow中實(shí)現(xiàn)去除不足一個(gè)batch的數(shù)據(jù)

    在tensorflow中實(shí)現(xiàn)去除不足一個(gè)batch的數(shù)據(jù)

    今天小編就為大家分享一篇在tensorflow中實(shí)現(xiàn)去除不足一個(gè)batch的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • 1分鐘快速生成用于網(wǎng)頁(yè)內(nèi)容提取的xslt

    1分鐘快速生成用于網(wǎng)頁(yè)內(nèi)容提取的xslt

    這篇文章主要教大家如何1分鐘快速生成用于網(wǎng)頁(yè)內(nèi)容提取的xslt,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 使用apidoc管理RESTful風(fēng)格Flask項(xiàng)目接口文檔方法

    使用apidoc管理RESTful風(fēng)格Flask項(xiàng)目接口文檔方法

    下面小編就為大家分享一篇使用apidoc管理RESTful風(fēng)格Flask項(xiàng)目接口文檔方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • 利用Python讀取txt文檔的方法講解

    利用Python讀取txt文檔的方法講解

    今天小編就為大家分享一篇利用Python讀取txt文檔的方法講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Python實(shí)現(xiàn)將數(shù)據(jù)庫(kù)一鍵導(dǎo)出為Excel表格的實(shí)例

    Python實(shí)現(xiàn)將數(shù)據(jù)庫(kù)一鍵導(dǎo)出為Excel表格的實(shí)例

    下面小編就為大家?guī)?lái)一篇Python實(shí)現(xiàn)將數(shù)據(jù)庫(kù)一鍵導(dǎo)出為Excel表格的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • 淺談django中的認(rèn)證與登錄

    淺談django中的認(rèn)證與登錄

    下面小編就為大家?guī)?lái)一篇淺談django中的認(rèn)證與登錄。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • Python的進(jìn)程,線程和協(xié)程實(shí)例詳解

    Python的進(jìn)程,線程和協(xié)程實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了Python進(jìn)程,線程和協(xié)程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • Python argparse命令參數(shù)與config配置參數(shù)示例深入詳解

    Python argparse命令參數(shù)與config配置參數(shù)示例深入詳解

    這篇文章主要介紹了Python argparse命令參數(shù)與config配置參數(shù),argparse是Python內(nèi)置的一個(gè)用于命令項(xiàng)選項(xiàng)與參數(shù)解析的模塊,通過(guò)在程序中定義好我們需要的參數(shù),然后在程序啟動(dòng)命令行傳遞我們想要改變的參數(shù)
    2023-03-03

最新評(píng)論