Python爬虫新手入门教学(五):爬取B站视频弹幕

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具备任何商业用途,若有问题请及时联系咱们以做处理。html

 

前文内容

Python爬虫新手入门教学(一):爬取豆瓣电影排行信息python

Python爬虫新手入门教学(二):爬取小说web

Python爬虫新手入门教学(三):爬取链家二手房数据json

Python爬虫新手入门教学(四):爬取前程无忧招聘信息api

Python爬虫、数据分析、网站开发等案例教程视频免费在线观看cookie

https://space.bilibili.com/523606542

基本开发环境

  • Python 3.6
  • Pycharm

相关模块的使用

  • requests
  • re

安装Python并添加到环境变量,pip安装须要的相关模块便可。网络

1、明确需求

找一个弹幕比较多的视频爬取ide

Python爬虫新手入门教学(五):爬取B站视频弹幕

 

Python爬虫新手入门教学(五):爬取B站视频弹幕

 

2、网页数据分析

之前的B站弹幕视频,点击查看历史的弹幕,会给你返回一个json数据,包含了全部的弹幕内容。
如今点击历史弹幕数据,一样是有数据加载出来,可是里面的都是乱码了。post

Python爬虫新手入门教学(五):爬取B站视频弹幕

 

Python爬虫新手入门教学(五):爬取B站视频弹幕

 


请求这个连接仍是会获得想要的数据内容。学习

Python爬虫新手入门教学(五):爬取B站视频弹幕

 


只须要使用正则表达匹配中文字符就能够匹配出来

3、解析数据并多页爬取

弹幕分页是根据日期来的,当点击 2021-01-01 的使用,返回的给个人数据并非弹幕数据,而是全部的日期。

Python爬虫新手入门教学(五):爬取B站视频弹幕

 


那么看到这里有人就会问了,那我想要爬取 2021-01-01 的弹幕数据怎么办?

Python爬虫新手入门教学(五):爬取B站视频弹幕

 


这两个的url地址是不同的,seg.so 才是弹幕数据url地址。

import requests import re def get_response(html_url): headers = { 'cookie': '你本身的cookie', 'origin': 'https://www.bilibili.com', 'referer': 'https://www.bilibili.com/video/BV19E41197Kc', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', } response = requests.get(url=html_url, headers=headers) return response def get_date(html_url): response = get_response(html_url) json_data = response.json() date = json_data['data'] print(date) return date if __name__ == '__main__': one_url = 'https://api.bilibili.com/x/v2/dm/history/index?type=1&oid=120004475&month=2021-01' get_date(one_url)

返回的数据是json数据,根据字典键值对取值就能够获得相关数据。

Python爬虫新手入门教学(五):爬取B站视频弹幕

 

4、保存数据(数据持久化)

def main(html_url): data = get_date(html_url) for date in data: url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=120004475&date={date}' html_data = get_response(url).text result = re.findall(".*?([\u4E00-\u9FA5]+).*?", html_data) for i in result: with open('B站弹幕.txt', mode='a', encoding='utf-8') as f: f.write(i) f.write('\n')
Python爬虫新手入门教学(五):爬取B站视频弹幕

 

5、完整代码

import requests import re def get_response(html_url): headers = { 'cookie': '你本身的cookie', 'origin': 'https://www.bilibili.com', 'referer': 'https://www.bilibili.com/video/BV19E41197Kc', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', } response = requests.get(url=html_url, headers=headers) return response def get_date(html_url): response = get_response(html_url) json_data = response.json() date = json_data['data'] print(date) return date def save(content): for i in content: with open('B站弹幕.txt', mode='a', encoding='utf-8') as f: f.write(i) f.write('\n') print(i) def main(html_url): data = get_date(html_url) for date in data: url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=120004475&date={date}' html_data = get_response(url).text result = re.findall(".*?([\u4E00-\u9FA5]+).*?", html_data) save(result) if __name__ == '__main__': one_url = 'https://api.bilibili.com/x/v2/dm/history/index?type=1&oid=120004475&month=2021-01' main(one_url)

总结

一、须要登录才能查看历史弹幕,爬取时须要携带cookie

二、能够保存到Excel里面,本文是保存txt文本

三、保存数据以后能够作词云分析下篇文章再说吧

相关文章
相关标签/搜索