python爬取b站搜索结果_【爬虫】简单爬取b站的弹幕列表

最近有朋友在群上面说做个b站某视频的弹幕统计列表,筛选出弹幕最多的那条!那么如何解决这个问题呢?首先肯定是要获取弹幕的列表吧,然后再进行分析吧。筛选出弹幕最多的那条,这个好办用collections可以解决,那么关键的问题应该就在怎么获取b站的弹幕列表了吧。

开发环境:

window7+chrome

idea+python插件

requests+json+beautifulsoup+collections

那解决步骤如下:

解析视频播放页面链接

查找弹幕资源所在地

数据分析 (collections.Counter)

数据存储(file)

导出exe

解析视频播放页资源,查找弹幕资源所在

查看下html网页源文件是否包含着弹幕的资源快捷键ctrl+u,选择其中的弹幕内容,发现不存在

按f12+选择network这个tab查看页面加载的时候的js链接处理

933a5a9aba52?from=timeline@

我们发现弹幕的内容包括在一个xml文件里面,对,而且还没有分页的处理

933a5a9aba52?from=timeline@

那么下一步就是找到这个数字所对应的链接了

933a5a9aba52?from=timeline@

https://api.bilibili.com/x/player/pagelist?aid={}&jsonp=jsonp.format("av号")

定代码架构:

def get_movie_url(av):pass #获取cid

def get_barrage_list(url):pass #获取列表

def get_barrage_count(list):pass #获取筛选数值

def write_text(content):pass #写进文本

写py代码

l_list = []

def get_movie_url(av):

response = requests.get(url_get_comment_cid.format(str(av)))

content = response.content.decode(response.encoding)

content = json.loads(content)

for d in content.get('data'):

cid = d.get('cid')

get_content(cid)

print("共有{}条弹幕".format(len(l_list)))

count = collections.Counter(l_list).most_common()

writetest(av, l_list)

writetest(str(av) + "count", count)

def get_barrage_list(cid):

response = requests.get(url_get_comment.format(str(cid)))

content = response.content.decode(response.encoding)

b = bs(content, 'xml')

for i in b.findChildren('d'):

l_list.append(i.text)

def write_text(av, l):

with open(str(av) + '.txt', 'w+', encoding='utf-8') as f:

for i in l:

f.write(str(i)+"\n")

使用pyInstanller导出exe

安装pyInstaller ,pip install pyinstaller 最新版本是3.3.1

pyinstaller -F py文件路径

生成exe文件,打开运行,至于下一步想导入excel

933a5a9aba52?from=timeline@

好的,就这样写完了。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值