Python | 巧用Python实现B站弹幕爬取系统!

 

 

 

 

 

在B站挖了一个月洞无果之后,打算还是要对B站做出点什么,于是我盯上了他们的弹幕系统!没错就是那名不虚传的B弹!

Python资源共享群:484031800

 

 

 

 

没错今天我们就是要来爬取B站的视频弹幕!

爬取B站弹幕说着简单实现难,你不仅仅要找到弹幕视频的api接口

还要掌握一系列的表达式及编程语法

但是

我并不慌,身为一名没多少进ICU的996患者(误)

让我们开始coding吧!

 

 

 

 

使用语言:Python3.7

在开始编写之前我们先对B站弹幕系统进行一个了解

 

 

 

 

 

我们先使用一点技术手段将B站弹幕系统单独分开来

然后打开审查元素进行api接口的查询

 

 

 

 

这里我先发一条弹幕

可以看到网络捕捉到了一条post的请求方式

点开之后逐步查询

 

 

 

 

成功找到接口!

实际上在GitHUb上bilibili的api接口就已经被公布了

但是作者认为还是手动去找比较靠谱一点

可以看到接口返回的都是我们要爬取的弹幕

我们打开Python,导入requests库

re库!

注意爬取弹幕要用到正则表达式到后面我会说明

我们先把几个模块导入进去

  •  
import requests as reqimport re

然后开始编写循环,毕竟不能只爬一次对吧

  •  
import requests as reqimport rewhile True:  video_number = input("av:")

条件触发video_number 在这里数值很重要 所以有必要加入一个条件判断

  •  
import requests as reqimport rewhile True:  video_number = input("av:")  if video_number == int:    print("int type");  else:

接下来就相当于字符拼接,这里我就不多说明了...

  •  
import requests as reqimport rewhile True:  video_number = input("av:")  if video_number == int:    print("int type");  else:    api_key = "https://api.bilibili.com/x/v1/dm/list.so?oid="+video_number    repon = req.get(api_key) #获取api    repon = encoding = "utf-8" #将编码转换为utf-8    xml = res.text 

基本上响应都成功

接下来我们开始编写正则表达式

先看下xml里面的数据

 

 

 

 

从<d 开头我们开始截取到 >然后中间就是我们要进行获取的值

后面跟上</d>和xml变量

具体代码

  •  
re.findall("<d.*?>(.*?)</d>",xml)

然后接下来我们对其赋值变量,打印看看

  •  
import requests as reqimport rewhile True:    avnumber = input("av:")    if avnumber == int:        print("OK")    else:        urlapi = "https://api.bilibili.com/x/v1/dm/list.so?oid="+avnumber        res = req.get(urlapi)        res.encoding = "utf-8"        xmlString = res.text        danmukus = re.findall("<d.*?>(.*?)</d>",xmlString)

 

 

 

 

 

神奇的bilibili

这样子看起来不太美观我们for循环输出

 

 

 

 

 

 

 

 

啊~舒服多了

拜拜

 

Python爬取B视频的弹幕并制作成地图词云展示,需要几个步骤:首先抓取网页数据,然后处理弹幕内容,最后生成词云并将其放在地图上。这里是一个简化的示例,假设我们使用了`requests`、`BeautifulSoup`库用于网络请求和HTML解析,`lxml`库作为BeautifulSoup的一个替代选择,`wordcloud`和`geopandas`库来创建词云和地图,以及`pyecharts`库来展示地图。 ```python import requests from bs4 import BeautifulSoup import re import pandas as pd from wordcloud import WordCloud import geopandas as gpd from pyecharts import options as opts from pyecharts.charts import Map # 爬取B弹幕数据 def get_bilibili_danmu(url): response = requests.get(url) soup = BeautifulSoup(response.text, 'lxml') danmu_list = soup.select('.d-share .d-mid .c-danmu-item') # 提取丹字列表 return [danmu.text for danmu in danmu_list] # 清理并统计词频 def process_danmu(danmus): cleaned_danmus = [re.sub(r'\W+', '', danmu) for danmu in danmus] # 去除标点符号和空格 frequency = {word: cleaned_danmus.count(word) for word in set(cleaned_danmus)} return frequency # 创建词云 def create_wordcloud(frequency): wc = WordCloud(font_path='simhei.ttf', width=800, height=600, background_color='white').generate_from_frequencies(frequency) return wc.to_image() # 获取中国省份信息 china_province_data = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) # 使用词云数据绘制地图 def map_with_wordcloud(province_data, wordcloud_img): chart = Map() chart.add("", province_data['name'], china_province_data.geometry, maptype="China") img_option = opts.ImageOption(src=wordcloud_img) chart.options设置 = { "visualMap": { "max": max(frequency.values()), "inRange": {"symbolSize": [5, 10]}, }, "series": [{"type": "map", "label": {"show": False}, "itemStyle": img_option}] } chart.render("bilibili_danmu_map.html") # 示例用法 url = "https://www.bilibili.com/video/BV某个视频ID" # 替换为实际的视频URL danmus = get_bilibili_danmu(url) frequency = process_danmu(danmus) wordcloud_img = create_wordcloud(frequency) map_with_wordcloud(china_province_data, wordcloud_img)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值