<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          用Python爬取B站、騰訊視頻、芒果TV和愛奇藝視頻彈幕

          共 6305字,需瀏覽 13分鐘

           ·

          2020-09-29 18:44

          點(diǎn)擊上方數(shù)據(jù)管道”,選擇“置頂星標(biāo)”公眾號(hào)

          干貨福利,第一時(shí)間送達(dá)


          眾所周知,彈幕,即在網(wǎng)絡(luò)上觀看視頻時(shí)彈出的評(píng)論性字幕。不知道大家看視頻的時(shí)候會(huì)不會(huì)點(diǎn)開彈幕,于我而言,彈幕是視頻內(nèi)容的良好補(bǔ)充,是一個(gè)組織良好的評(píng)論序列。通過分析彈幕,我們可以快速洞察廣大觀眾對(duì)于視頻的看法。?

          J哥通過一個(gè)關(guān)于《八佰》的視頻彈幕數(shù)據(jù),繪制了如下詞云圖,感覺效果還是可以的。?


          海量的彈幕數(shù)據(jù)不僅可以繪制此類詞云圖,還可以調(diào)用百度AI進(jìn)行情感分析。那么,我們?cè)撊绾潍@取彈幕數(shù)據(jù)呢?本文運(yùn)用Python爬取B站視頻、騰訊視頻、芒果TV和愛奇藝視頻等彈幕,讓你輕松獲取主流視頻網(wǎng)站彈幕數(shù)據(jù)。?

          一、B站視頻彈幕

          1.網(wǎng)頁分析

          本文以爬取up主硬核的半佛仙人發(fā)布的《你知道奶茶加盟到底有多坑人嗎?》視頻彈幕為例,首先通過以下步驟找到存放彈幕的真實(shí)url。




          簡單分析url參數(shù),很顯然,date參數(shù)表示發(fā)送彈幕的時(shí)間,其他參數(shù)均無變化。因此,只需要改變date參數(shù),然后通過beautifulsoup解析到彈幕數(shù)據(jù)即可。
          2.爬蟲實(shí)戰(zhàn)
          import?requests?#請(qǐng)求網(wǎng)頁數(shù)據(jù)
          from?bs4?import?BeautifulSoup?#美味湯解析數(shù)據(jù)
          import?pandas?as?pd
          import?time
          from?tqdm?import?trange?#獲取爬取速度

          def?get_bilibili_url(start,?end):
          ????url_list?=?[]
          ????date_list?=?[i?for?i?in?pd.date_range(start,?end).strftime('%Y-%m-%d')]
          ????for?date?in?date_list:
          ????????url?=?f"https://api.bilibili.com/x/v2/dm/history?type=1&oid=141367679&date={date}"
          ????????url_list.append(url)
          ????return?url_list

          def?get_bilibili_danmu(url_list):
          ????headers?=?{
          ????????"user-agent":?"Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?10_14_6)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/83.0.4103.116?Safari/537.36",
          ????????"cookie":?"你自己的"?#Headers中copy即可
          ???????????????}

          ????file?=?open("bilibili_danmu.txt",?'w')
          ????for?i?in?trange(len(url_list)):
          ????????url?=?url_list[i]
          ????????response?=?requests.get(url,?headers=headers)
          ????????response.encoding?=?'utf-8'
          ????????soup?=?BeautifulSoup(response.text)
          ????????data?=?soup.find_all("d")
          ????????danmu?=?[data[i].text?for?i?in?range(len(data))]
          ????????for?items?in?danmu:
          ????????????file.write(items)
          ????????????file.write("\n")
          ????????time.sleep(3)
          ????file.close()


          if?__name__?==?"__main__":
          ????start?=?'9/24/2020'?#設(shè)置爬取彈幕的起始日
          ????end?=?'9/26/2020'?#設(shè)置爬取彈幕的終止日
          ????url_list?=?get_bilibili_url(start,?end)
          ????get_bilibili_danmu(url_list)
          ????print("彈幕爬取完成")

          3.數(shù)據(jù)預(yù)覽



          二、騰訊視頻彈幕

          1.網(wǎng)頁分析

          本文以爬取《脫口秀大會(huì)?第3季》最后一期視頻彈幕為例,首先通過以下步驟找到存放彈幕的真實(shí)url。



          通過刪減各參數(shù),發(fā)現(xiàn)僅有timestamp參數(shù)的變化會(huì)影響彈幕數(shù)據(jù)的爬取,且timestamp參數(shù)是首項(xiàng)為15,公差為30的等差數(shù)列。可以大膽猜測騰訊視頻每30秒更新一頁彈幕數(shù)據(jù),該視頻長度為12399秒。而數(shù)據(jù)格式為標(biāo)準(zhǔn)的json格式,因此json.loads直接解析數(shù)據(jù)即可。
          2.爬蟲實(shí)戰(zhàn)
          import?requests
          import?json
          import?time
          import?pandas?as?pd

          df?=?pd.DataFrame()
          for?page?in?range(15,?12399,?30):
          ????headers?=?{'User-Agent':?'Mozilla/5.0?(Windows?NT?6.1;?WOW64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/63.0.3239.132?Safari/537.36'}
          ????url?=?'https://mfm.video.qq.com/danmu?otype=json×tamp={}&target_id=5938032297%26vid%3Dx0034hxucmw&count=80'.format(page)
          ????print("正在提取第"?+?str(page)?+?"頁")
          ????html?=?requests.get(url,headers?=?headers)
          ????bs?=?json.loads(html.text,strict?=?False)??#strict參數(shù)解決部分內(nèi)容json格式解析報(bào)錯(cuò)
          ????time.sleep(1)
          ????#遍歷獲取目標(biāo)字段
          ????for?i?in?bs['comments']:
          ????????content?=?i['content']??#彈幕
          ????????upcount?=?i['upcount']??#點(diǎn)贊數(shù)
          ????????user_degree?=i['uservip_degree']?#會(huì)員等級(jí)
          ????????timepoint?=?i['timepoint']??#發(fā)布時(shí)間
          ????????comment_id?=?i['commentid']??#彈幕id
          ????????cache?=?pd.DataFrame({'彈幕':[content],'會(huì)員等級(jí)':[user_degree],
          ??????????????????????????????'發(fā)布時(shí)間':[timepoint],'彈幕點(diǎn)贊':[upcount],'彈幕id':[comment_id]})
          ????????df?=?pd.concat([df,cache])
          df.to_csv('tengxun_danmu.csv',encoding?=?'utf-8')
          print(df.shape)

          3.數(shù)據(jù)預(yù)覽


          三、芒果TV彈幕
          1.網(wǎng)頁分析
          本文以爬取《乘風(fēng)破浪的姐姐》最后一期視頻彈幕為例,首先通過以下步驟找到存放彈幕的真實(shí)url。


          通過分析參數(shù),我們可以發(fā)現(xiàn),芒果TV會(huì)生成首項(xiàng)為0,公差為1的等差數(shù)列json彈幕文件,每個(gè)json文件存儲(chǔ)前一分鐘內(nèi)所有的彈幕數(shù)據(jù)。彈幕數(shù)據(jù)存放格式為json,數(shù)據(jù)解析較為簡單。

          2.爬蟲實(shí)戰(zhàn)
          import?requests
          import?json
          import?pandas?as?pd

          def?get_mangguo_danmu(num1,?num2,?page):
          ????try:
          ????????url?=?'https://bullet-ws.hitv.com/bullet/2020/09/29/{}/{}/{}.json'
          ????????print("正在爬取第"?+?str(page)?+?"頁")
          ????????danmuurl?=?url.format(num1,?num2,?page)
          ????????res?=?requests.get(danmuurl)
          ????????res.encoding?=?'utf-8'
          ????????#print(res.text)
          ????????data?=?json.loads(res.text)
          ????except:
          ????????print("無法連接")

          ????details?=?[]
          ????for?i?in?range(len(data['data']['items'])):??#?彈幕數(shù)據(jù)在json文件'data'的'items'中
          ????????result?=?{}
          ????????result['stype']?=?num2??#?通過stype可識(shí)別期數(shù)
          ????????result['id']?=?data['data']['items'][i]['id']??#?獲取id

          ????????try:??#?嘗試獲取uname
          ????????????result['uname']?=?data['data']['items'][i]['uname']
          ????????except:
          ????????????result['uname']?=?''

          ????????result['content']?=?data['data']['items'][i]['content']??#?獲取彈幕內(nèi)容
          ????????result['time']?=?data['data']['items'][i]['time']??#?獲取彈幕發(fā)布時(shí)間

          ????????try:??#?嘗試獲取彈幕點(diǎn)贊數(shù)
          ????????????result['v2_up_count']?=?data['data']['items'][i]['v2_up_count']
          ????????except:
          ????????????result['v2_up_count']?=?''
          ????????details.append(result)

          ????return?details

          #輸入關(guān)鍵信息
          def?count_danmu():
          ????danmu_total?=?[]
          ????num1?=?input('第一個(gè)數(shù)字')
          ????num2?=?input('第二個(gè)數(shù)字')
          ????page?=?int(input('輸入總時(shí)長'))
          ????for?i?in?range(page):
          ????????danmu_total.extend(get_mangguo_danmu(num1,?num2,?i))

          ????return?danmu_total

          def?main():
          ????df?=?pd.DataFrame(count_danmu())
          ????df.to_csv('mangguo_danmu.csv')

          if?__name__?==?'__main__':
          ????main()

          3.數(shù)據(jù)預(yù)覽


          四、愛奇藝彈幕

          1.網(wǎng)頁分析

          本文以爬取《樂隊(duì)的夏天第2季》第13期上視頻彈幕為例,首先通過以下步驟找到存放彈幕的真實(shí)url。



          分析彈幕真實(shí)url,我們發(fā)現(xiàn),參數(shù)5981449914376200是視頻tvid,參數(shù)62是tvid倒數(shù)4為的前兩位,參數(shù)00是tvid的最后兩位,.z前的參數(shù)1為視頻總時(shí)長除以300秒向上取整。觀察相鄰兩個(gè)彈幕文件包,可以看出愛奇藝每5分鐘更新一次彈幕文件。


          由于直接爬取出來的彈幕文件存在亂碼,需要進(jìn)行二進(jìn)制編碼,方可得到最終的彈幕數(shù)據(jù)。

          2.爬蟲實(shí)戰(zhàn)


          import?zlib
          import?requests

          #?1.爬取xml文件
          def?download_xml(url):
          ????bulletold?=?requests.get(url).content??#?二進(jìn)制內(nèi)容
          ????return?zipdecode(bulletold)

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

          for?x?in?range(1,12):
          ????#?x是從1到12,12怎么來的,這一集總共57分鐘,愛奇藝每5分鐘會(huì)加載新的彈幕,57除以5向上取整
          ????url?=?'https://cmts.iqiyi.com/bullet/62/00/5981449914376200_300_'?+?str(x)?+?'.z'
          ????xml?=?download_xml(url)
          ????#?把編碼好的文件分別寫入17個(gè)xml文件中(類似于txt文件),方便后邊取數(shù)據(jù)
          ????with?open('./aiqiyi/iqiyi'?+?str(x)?+?'.xml',?'a+',?encoding='utf-8')?as?f:
          ????????f.write(xml)

          #?2.讀取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:
          ????????content?=?entry.getElementsByTagName('content')[0]
          ????????print(content.childNodes[0].data)
          ????????i?=?content.childNodes[0].data
          ????????result.append(i)
          ????return?result

          with?open("aiyiqi_danmu.txt",?mode="w",?encoding="utf-8")?as?f:
          ????for?x?in?range(1,12):
          ????????l?=?xml_parse("./aiqiyi/iqiyi"?+?str(x)?+?".xml")
          ????????for?line?in?l:
          ????????????f.write(line)
          ????????????f.write("\n"

          3.數(shù)據(jù)預(yù)覽


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

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  草逼草综合 | 日韩黄色电影网址网站 | 精品无码免费一区二区 | 日本中文乱伦字幕 | 翔田千里无码免费播放 |