B站热度榜视频信息爬虫

发布时间:2026/7/9 2:29:21
B站热度榜视频信息爬虫 一.前言1.功能描述本脚本爬取B站当前热度榜排名第一的视频详细信息包括点赞数、投币数、收藏数、分享数、评论数以及评论区点赞最高的评论。2.依赖模块requests用于发送HTTP请求二.具体步骤1.引入库定义请求头import requests import os import time from openpyxl import Workbook from openpyxl.styles import Font, Alignment, PatternFill headers { User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36, Referer: https://www.bilibili.com/, Origin: https://www.bilibili.com }2.API配置hot_api https://api.bilibili.com/x/web-interface/popular view_api https://api.bilibili.com/x/web-interface/view reply_api https://api.bilibili.com/x/v2/reply3.定义函数定义获取视频信息视频统计和top评论函数def get_hot_video_info(): try: response requests.get(hot_api, headersheaders, timeout15) response.raise_for_status() data response.json() if data[code] ! 0: print(fAPI错误: {data.get(message, 未知错误)}) return items data[data].get(list, []) if not items: print(未获取到视频数据) return video items[0] bvid video.get(bvid) aid video.get(aid) stat_info get_video_stat(bvid) if not stat_info: return reply_info get_top_comment(aid) video_data { 标题: video.get(title, ), UP主: video.get(owner, {}).get(name, ), BVID: bvid, AID: aid, 点赞数: stat_info[like], 投币数: stat_info[coin], 收藏数: stat_info[favorite], 分享数: stat_info[share], 评论数: stat_info[reply], 热门评论作者: reply_info[author], 热门评论点赞数: reply_info[like], 热门评论内容: reply_info[content] } save_to_excel(video_data) except Exception as e: print(f获取失败: {e}) def get_video_stat(bvid): try: response requests.get(view_api, headersheaders, params{bvid: bvid}, timeout15) response.raise_for_status() data response.json() if data[code] ! 0: print(f获取统计失败: {data.get(message, 未知错误)}) return None stat data[data].get(stat, {}) return { like: stat.get(like, 0), coin: stat.get(coin, 0), favorite: stat.get(favorite, 0), share: stat.get(share, 0), reply: stat.get(reply, 0) } except Exception as e: print(f统计异常: {e}) return None def get_top_comment(aid): try: params {type: 1, oid: aid, pn: 1, ps: 20, sort: 1} response requests.get(reply_api, headersheaders, paramsparams, timeout15) response.raise_for_status() data response.json() if data[code] ! 0: return {content: 获取评论失败, author: , like: 0} replies data.get(data, {}).get(replies, []) if not replies: return {content: 暂无评论, author: , like: 0} top_reply replies[0] return { content: top_reply.get(content, {}).get(message, ), author: top_reply.get(member, {}).get(uname, ), like: top_reply.get(like, 0) } except Exception as e: return {content: 获取评论异常, author: , like: 0}4.保存为excel文件保存样式第二行内容设置为水平垂直居中行高50自动换行def save_to_excel(data): try: output_dir os.path.join(os.path.dirname(__file__), output) os.makedirs(output_dir, exist_okTrue) filepath os.path.join(output_dir, fbilibili_hot_video_{time.strftime(%Y%m%d_%H%M%S)}.xlsx) wb Workbook() ws wb.active ws.title B站热门视频 column_headers [标题, UP主, BVID, AID, 点赞数, 投币数, 收藏数, 分享数, 评论数, 热门评论作者, 热门评论点赞数, 热门评论内容] header_font Font(boldTrue, colorFFFFFF) header_fill PatternFill(start_color4472C4, end_color4472C4, fill_typesolid) for col, header in enumerate(column_headers, 1): cell ws.cell(row1, columncol, valueheader) cell.font header_font cell.fill header_fill cell.alignment Alignment(horizontalcenter, verticalcenter) data_alignment Alignment(horizontalcenter, verticalcenter, wrap_textTrue) for col, key in enumerate(column_headers, 1): cell ws.cell(row2, columncol, valuedata.get(key, )) cell.alignment data_alignment ws.row_dimensions[2].height 50 for col in range(1, len(column_headers) 1): ws.column_dimensions[ws.cell(row1, columncol).column_letter].width 20 wb.save(filepath) print(f保存成功: {filepath}) except Exception as e: print(f保存失败: {e}) if __name__ __main__: get_hot_video_info()保存结果如下三.总结项目借助 requests 库调用 B 站开放 API请求 B 站热门榜单接口获取排行首位热门视频基础信息提取视频 BVID、AID通过视频详情接口爬取该视频点赞、投币、收藏等互动统计数据调用评论接口按热度排序获取最高赞一级评论的作者、点赞量与评论内容随后使用 openpyxl 创建带样式的 Excel 文件设置样式让表格尽量美观。并设置有各类异常并打印提示最终实现单条 B 站榜首热门视频完整信息自动化爬取与Excel 保存。