1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        用 Python 爬取『贅婿』視頻彈幕

        共 2921字,需瀏覽 6分鐘

         ·

        2021-03-12 10:03

        1

        前言



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


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



        2

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


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

        在瀏覽器里面按F12



        找到這類url


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


        2.分析彈幕鏈接


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


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


        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.zhttps://cmts.iqiyi.com/bullet/54/00/7973227714515400_300_2.zhttps://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)







        3

        解析xml

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


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


        #讀取xml文件中的彈幕數(shù)據(jù)數(shù)據(jù)from xml.dom.minidom import parseimport xml.dom.minidomdef 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)




        4

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


        1.保存前工作

        import xlwt# 創(chuàng)建一個(gè)workbook 設(shè)置編碼workbook = xlwt.Workbook(encoding = 'utf-8')# 創(chuàng)建一個(gè)worksheetworksheet = 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')




        5

        總結(jié)


        1.通過實(shí)戰(zhàn)案『贅婿』,手把手實(shí)現(xiàn)python爬取愛奇藝彈幕。

        2.python解析xml格式數(shù)據(jù)。


        3.將數(shù)據(jù)寫入excel。



        更多閱讀



        2020 年最佳流行 Python 庫 Top 10


        2020 Python中文社區(qū)熱門文章 Top 10


        5分鐘快速掌握 Python 定時(shí)任務(wù)框架

        特別推薦




        點(diǎn)擊下方閱讀原文加入社區(qū)會(huì)員

        瀏覽 70
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            狠狠干奇米 | 亚洲污| 久久人人爽人人爽人人片亞洲 | 美女黄色一级片 | 狠狠人妻久久久久久综合蜜桃 | 在线看逼| 亚洲AAA视频 | 人妻偷拍视频 | 精品久久一区 | 婷婷五月天中文字幕 |