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>

        以『贅婿』為實戰(zhàn)案例,手把手教會你用python爬取『愛奇藝』視頻彈幕

        共 3111字,需瀏覽 7分鐘

         ·

        2021-03-06 02:47

        ↑ 關(guān)注 + 星標(biāo) ,每天學(xué)Python新技能

        后臺回復(fù)【大禮包】送你Python自學(xué)大禮包


        1

        前言



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


        由于為了讓小白徹底學(xué)會使用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,才是有用的!?。?!


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


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


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

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


        愛奇藝每5分鐘會加載新的彈幕,每一集約是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):    '對zip壓縮的二進(jìn)制內(nèi)容解碼成文本'    decode = zlib.decompress(bytearray(bulletold), 15 + 32).decode('utf-8')    return decode


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


        # 把編碼好的文件分別寫入個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.評論內(nèi)容content)、3.評論點贊數(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)建一個workbook 設(shè)置編碼workbook = xlwt.Workbook(encoding = 'utf-8')# 創(chuàng)建一個worksheetworksheet = workbook.add_sheet('sheet1')
        # 寫入excel# 參數(shù)對應(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ù)對應(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ù)集-李運辰.xls

        for x in range(1,11):    l = xml_parse("./lyc/zx" + str(x) + ".xml")
        # 保存workbook.save('彈幕數(shù)據(jù)集-李運辰.xls')




        5

        總結(jié)


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

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


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



        如果想學(xué)習(xí)本文代碼


        點擊上方公眾號名片,回復(fù)「愛奇藝彈幕」獲取完整代碼。




        送大家一份Python學(xué)習(xí)大禮包,從Python基礎(chǔ),爬蟲,數(shù)據(jù)分析Web開發(fā)等全套資料,吃透資料,
        這些資料都是視頻,新人學(xué)起來非常友好。



        掃碼加微信后備注「Python新手」方便我給你發(fā)送資料



        推薦閱讀

        1. 限時刪除!B站最好的Python+數(shù)據(jù)分析精華匯總

        2. 臥槽,一個牛逼的Python 可視化庫:PyG2Plot

        3. IT大佬廖雪峰帶你玩轉(zhuǎn)Python數(shù)據(jù)分析(內(nèi)附資源)


        掃碼回復(fù)「大禮包」后獲取大禮

        瀏覽 102
        點贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        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>
            国产精品自拍不卡 | 黄色动漫最新动漫免费观看 | 波多野结衣高清av久久直播免 | 色五月首页 | 黄工厂精品视频免费观看 | 国产a网站 | 熟女在线视频 | www 黄 欧美 | 欧美曰韩精品 | 丰满少妇高潮久久久久久 |