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

前言

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

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

https://space.bilibili.com/523606542

前文内容

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

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

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

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

基本开发环境

  • Python 3.6
  • Pycharm

相关模块的使用

  • requests
  • re

安装Python并添加到环境变量,pip安装需要的相关模块即可。

一、明确需求

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

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

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

二、网页数据分析

以前的B站弹幕视频,点击查看历史的弹幕,会给你返回一个json数据,包含了所有的弹幕内容。
现在点击历史弹幕数据,同样是有数据加载出来,但是里面的都是乱码了。

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

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


请求这个链接还是会得到想要的数据内容。

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


只需要使用正则表达匹配中文字符就可以匹配出来

三、解析数据并多页爬取

弹幕分页是根据日期来的,当点击 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站视频弹幕

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

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站视频弹幕

五、完整代码

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)

总结

1、需要登陆才能查看历史弹幕,爬取时需要携带cookie

2、可以保存到Excel里面,本文是保存txt文本

3、保存数据之后可以做词云分析下篇文章再说吧

猜你喜欢

转载自blog.csdn.net/m0_48405781/article/details/113183911