请写一段爬取b站视频弹幕的python爬虫代码
时间: 2023-03-12 09:00:03 浏览: 178
爬取b站视频弹幕的python爬虫代码:import requestsurl='https://api.bilibili.com/x/v1/dm/list.so?oid=视频id'headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36'
}res=requests.get(url,headers=headers)danmu=res.json()['dms']for item in danmu:
print(item['p'][2],item['c'])
相关问题
用Python语言编写一个爬取B站视频弹幕的爬虫程序
由于B站的弹幕是通过WebSocket实时传输的,因此我们需要使用Python的WebSocket模块来进行连接和接收数据。以下是一个简单的爬取B站视频弹幕的爬虫程序:
```python
import websocket
import json
def on_message(ws, message):
msg = json.loads(message)
if msg['cmd'] == 'DANMU_MSG':
print(msg['info'][2][1] + ': ' + msg['info'][1])
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
print("### connected ###")
ws.send('{"type": "loginreq","roomid": "弹幕所在的房间号"}')
ws.send('{"type": "joingroup","rid": "弹幕所在的房间号","gid": -9999}')
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://broadcastlv.chat.bilibili.com:2245/sub",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
```
需要注意的是,此程序需要使用websocket模块,可以通过pip安装:
```bash
pip install websocket-client
```
另外,需要将代码中的“弹幕所在的房间号”替换成目标视频的房间号。可以通过打开目标视频,查看地址栏中的“/video/”后面的数字即可得到。
请写一段python爬取B站视频弹幕发布的时间,并进行可视化的代码
好的,以下是一个爬取 B 站视频弹幕发布时间并进行可视化的 Python 代码:
```python
import datetime
import matplotlib.pyplot as plt
import bilibili_api
from bilibili_api import video
# 视频的 av 号
av_number = 123456
# 获取弹幕数据
danmaku_data = video.get_danmaku(av_number)
# 提取弹幕发布时间
danmaku_time = []
for danmaku in danmaku_data:
time_str = danmaku['time']
time_float = float(time_str)
time_stamp = datetime.datetime.fromtimestamp(time_float)
danmaku_time.append(time_stamp)
# 统计弹幕数量和发布时间
danmaku_count = []
danmaku_time_dict = {}
for time_stamp in danmaku_time:
time_str = time_stamp.strftime('%Y-%m-%d %H:00:00')
if time_str in danmaku_time_dict:
danmaku_time_dict[time_str] += 1
else:
danmaku_time_dict[time_str] = 1
for time_str in sorted(danmaku_time_dict.keys()):
danmaku_count.append(danmaku_time_dict[time_str])
# 绘制可视化图表
plt.figure(figsize=(12, 6))
plt.plot(danmaku_time_dict.keys(), danmaku_count)
plt.xlabel('发布时间')
plt.ylabel('弹幕数量')
plt.title('B 站视频弹幕发布时间分布')
plt.xticks(rotation=45, ha='right')
plt.show()
```
这段代码会首先通过 `Bilibili-API` 库获取指定视频的弹幕数据,然后提取弹幕发布时间,并统计每个小时内的弹幕数量。最后使用 `matplotlib` 库绘制发布时间和弹幕数量的可视化图表。需要注意的是,这里的时间是按小时统计的,如果需要按其他时间粒度统计,可以修改代码中的 `strftime` 方法参数。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)