怎么使用Python爬取視頻彈幕

今天就跟大家聊聊有關(guān)怎么使用Python爬取視頻彈幕,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、磐石ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的磐石網(wǎng)站制作公司

前言

之前愛奇藝獨(dú)播熱劇『贅婿』特別火,筆者也在一直追,借助手中的技術(shù),想爬取彈幕分析該劇的具體情況以及網(wǎng)友的評(píng)論!

怎么使用Python爬取視頻彈幕

由于為了讓小白徹底學(xué)會(huì)使用python爬取愛奇藝彈幕的技術(shù),因此本文詳細(xì)介紹如何進(jìn)行爬取,下文再進(jìn)行分析數(shù)據(jù)!

分析數(shù)據(jù)包

1.查找數(shù)據(jù)包

在瀏覽器里面按F12

找到這類url

https://cmts.iqiyi.com/bullet/54/00/7973227714515400_60_2_5f3b2e24.br

2.分析彈幕鏈接

其中的/54/00/7973227714515400,才是有用的?。。?!

愛奇藝的彈幕獲取地址如下:

https://cmts.iqiyi.com/bullet/參數(shù)1_300_參數(shù)2.z

參數(shù)1是:/54/00/7973227714515400

參數(shù)2是:數(shù)字1、2、3.....

愛奇藝每5分鐘會(huì)加載新的彈幕,每一集約是46分鐘,46除以5向上取整就是10

因此彈幕的鏈接如下:

https://cmts.iqiyi.com/bullet/54/00/7973227714515400_300_1.z
https://cmts.iqiyi.com/bullet/54/00/7973227714515400_300_2.z
https://cmts.iqiyi.com/bullet/54/00/7973227714515400_300_3.z
......
https://cmts.iqiyi.com/bullet/54/00/7973227714515400_300_10.z

3.解碼二進(jìn)制數(shù)據(jù)包

通過彈幕鏈接下載的彈幕包是以z為后綴格式的文件,需要進(jìn)行解碼!

def zipdecode(bulletold):
    '對(duì)zip壓縮的二進(jìn)制內(nèi)容解碼成文本'
    decode = zlib.decompress(bytearray(bulletold), 15 + 32).decode('utf-8')
    return decode

解碼之后將數(shù)據(jù)保存成xml格式

# 把編碼好的文件分別寫入個(gè)xml文件中(類似于txt文件),方便后邊取數(shù)據(jù)
  with open('./lyc/zx' + str(x) + '.xml', 'a+', encoding='utf-8') as f:
      f.write(xml)

怎么使用Python爬取視頻彈幕

解析xml

1.提取數(shù)據(jù)

怎么使用Python爬取視頻彈幕

通過查看xml文件,我們需要提取的內(nèi)容有1.用戶id(uid)、2.評(píng)論內(nèi)容(content)、3.評(píng)論點(diǎn)贊數(shù)(likeCount)。

#讀取xml文件中的彈幕數(shù)據(jù)數(shù)據(jù)
from xml.dom.minidom import parse
import xml.dom.minidom
def xml_parse(file_name):
    DOMTree = xml.dom.minidom.parse(file_name)
    collection = DOMTree.documentElement
    # 在集合中獲取所有entry數(shù)據(jù)
    entrys = collection.getElementsByTagName("entry")
    print(entrys)
    result = []
    for entry in entrys:
        uid = entry.getElementsByTagName('uid')[0]
        content = entry.getElementsByTagName('content')[0]
        likeCount = entry.getElementsByTagName('likeCount')[0]
        print(uid.childNodes[0].data)
        print(content.childNodes[0].data)
        print(likeCount.childNodes[0].data)

保存數(shù)據(jù)

1.保存前工作

import xlwt
# 創(chuàng)建一個(gè)workbook 設(shè)置編碼
workbook = xlwt.Workbook(encoding = 'utf-8')
# 創(chuàng)建一個(gè)worksheet
worksheet = workbook.add_sheet('sheet1')


# 寫入excel
# 參數(shù)對(duì)應(yīng) 行, 列, 值
worksheet.write(0,0, label='uid')
worksheet.write(0,1, label='content')
worksheet.write(0,2, label='likeCount')

導(dǎo)入xlwt庫(寫入csv),并定義好標(biāo)題(uid、content、likeCount)

2.寫入數(shù)據(jù)

for entry in entrys:
    uid = entry.getElementsByTagName('uid')[0]
    content = entry.getElementsByTagName('content')[0]
    likeCount = entry.getElementsByTagName('likeCount')[0]
    print(uid.childNodes[0].data)
    print(content.childNodes[0].data)
    print(likeCount.childNodes[0].data)
    # 寫入excel
    # 參數(shù)對(duì)應(yīng) 行, 列, 值
    worksheet.write(count, 0, label=str(uid.childNodes[0].data))
    worksheet.write(count, 1, label=str(content.childNodes[0].data))
    worksheet.write(count, 2, label=str(likeCount.childNodes[0].data))
    count=count+1

最后保存成彈幕數(shù)據(jù)集-李運(yùn)辰.xls

for x in range(1,11):
    l = xml_parse("./lyc/zx" + str(x) + ".xml")


# 保存
workbook.save('彈幕數(shù)據(jù)集-李運(yùn)辰.xls')

怎么使用Python爬取視頻彈幕

看完上述內(nèi)容,你們對(duì)怎么使用Python爬取視頻彈幕有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站題目:怎么使用Python爬取視頻彈幕
當(dāng)前路徑:http://bm7419.com/article2/jdgoic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、域名注冊(cè)微信公眾號(hào)、建站公司、Google、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司