自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python3爬取B站視頻彈幕

開(kāi)發(fā) 后端
本文通過(guò)8個(gè)步驟教你如何使用Python3爬取B站的視頻彈幕,快往下看看吧。

 [[215973]]

需要準(zhǔn)備的環(huán)境:

  1. 一個(gè)B站賬號(hào),需要先登錄,否則不能查看歷史彈幕記錄
  2. 聯(lián)網(wǎng)的電腦和順手的瀏覽器,我用的Chrome
  3. Python3環(huán)境以及request模塊,安裝使用命令,換源比較快:
  1. pip3 install  request -i http://pypi.douban.com/simple  

爬取步驟:

1.登錄后打開(kāi)需要爬取的視頻頁(yè)面,打開(kāi)開(kāi)發(fā)者工具臺(tái),Chrome可以使用F12快捷鍵,選擇network監(jiān)聽(tīng)請(qǐng)求  

 

2.點(diǎn)擊查看歷史彈幕,獲取請(qǐng)求  

 

  

其中rolldate后面的數(shù)字表示該視頻對(duì)應(yīng)的彈幕號(hào),返回的數(shù)據(jù)中timestamp表示彈幕日期,new表示數(shù)目 

 

4.在查看歷史彈幕中任選一天,查看,會(huì)發(fā)出新的請(qǐng)求

dmroll ,時(shí)間戳,彈幕號(hào),表示獲取該日期的彈幕,1507564800 表示2017/10/10 0:0:0 

 

 

 

 

 

該請(qǐng)求返回xml數(shù)據(jù) 

 

5.使用正則表達(dá)式獲取所有彈幕消息,匹配模式

 

  1. '<d p=".*?">(.*?)</d>'  

6.拼接字符串,將所有彈幕保存到本地文件即可 

  1. with open('content.txt', mode='w+', encoding='utf8'as f:    f.write(content)  

7.參考代碼如下,將彈幕按照日期保存為單個(gè)文件...因?yàn)樘嗔?.. 

 

  1. import requests 
  2.  
  3. import re 
  4.  
  5. import time  
  6.  
  7. """    爬取嗶哩嗶哩視頻彈幕信息"""  
  8.  
  9. # 2043618 是視頻的彈幕標(biāo)號(hào),這個(gè)地址會(huì)返回時(shí)間列表 
  10.  
  11. # https://www.bilibili.com/video/av1349282 
  12.  
  13. url = 'https://comment.bilibili.com/rolldate,2043618' 
  14.  
  15. # 獲取彈幕的id 2043618 
  16.  
  17. video_id = url.split(',')[-1]print(video_id) 
  18.  
  19. # 獲取json文件 
  20.  
  21. html = requests.get(url) 
  22.  
  23. # print(html.json()) 
  24.  
  25.   
  26.  
  27. # 生成時(shí)間戳列表 
  28.  
  29. time_list = [i['timestamp'for i in html.json()] 
  30.  
  31. # print(time_list) 
  32.  
  33.   
  34.  
  35. # 獲取彈幕網(wǎng)址格式 'https://comment.bilibili.com/dmroll,時(shí)間戳,彈幕號(hào)' 
  36.  
  37.   
  38.  
  39. # 彈幕內(nèi)容,由于總彈幕量太大,將每個(gè)彈幕文件分別保存 
  40.  
  41. for i in time_list:    content = ''    j = 'https://comment.bilibili.com/dmroll,{0},{1}'.format(i, video_id)    print(j)    text = requests.get(j).text 
  42.  
  43.     # 匹配彈幕內(nèi)容    res = re.findall('<d p=".*?">(.*?)</d>', text)     
  44.  
  45.     # 將時(shí)間戳轉(zhuǎn)化為日期形式,需要把字符串轉(zhuǎn)為整數(shù)    timeArray = time.localtime(int(i))    date_time = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)    print(date_time)    content += date_time + ' 
  46.  
  47. '    for k in res:        content += k + ' 
  48.  
  49. '    content += ' 
  50.  
  51. '    file_path = 'txt/{}.txt'.format(time.strftime("%Y_%m_%d", timeArray))    print(file_path)     
  52.  
  53.     with open(file_path, mode='w+', encoding='utf8'as f:        f.write(content)  

8.最終效果 

 

 

責(zé)任編輯:龐桂玉 來(lái)源: 程序員共讀
相關(guān)推薦

2017-11-17 19:56:46

爬蟲(chóng)視頻信息數(shù)據(jù)庫(kù)

2021-10-29 07:49:23

Python彈幕播放

2021-06-02 15:10:20

PythonScrapy視頻

2020-12-02 09:42:42

PythonApp抖音視頻

2021-09-09 06:18:04

交互功能彈幕

2022-12-26 00:00:05

Python爬蟲(chóng)B站彈幕

2025-03-05 00:00:55

2020-10-20 14:12:54

B站開(kāi)源彈幕

2025-04-28 08:59:25

2018-02-24 18:11:11

2020-10-12 08:19:43

Python爬蟲(chóng)網(wǎng)頁(yè)數(shù)據(jù)

2016-12-07 11:18:58

Python爬蟲(chóng)網(wǎng)站

2017-05-24 15:07:19

Python爬蟲(chóng)爬取

2020-11-03 14:10:45

Python爬取天氣爬蟲(chóng)

2019-01-02 12:23:30

Python金融數(shù)據(jù)爬取

2019-04-24 09:48:54

2024-03-01 18:52:31

視頻超分算法

2015-05-07 09:32:55

APP開(kāi)源

2021-03-01 21:32:49

HTTP2 QUIC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)