You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.7 KiB

import requests
import json
import csv
import os
# 创建文件夹
if not os.path.exists('comment_files'):
os.makedirs('comment_files')
# 读取视频链接CSV文件
with open('video_links.csv', newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
# 跳过表头
next(reader)
# 遍历每个视频链接
for row in reader:
title, video_url, cover_url = row
# 获取BV号
bv = video_url.split('/')[-1]
# 请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/58.0.3029.110 Safari/537.3'
}
# 请求URL
url = 'https://api.bilibili.com/x/v2/reply?type=1&oid=' + bv + '&pn='
# 评论总数
total_count = 0 # 默认值为0
# 遍历所有评论页
for page in range(1, total_count // 20 + 2):
# 发送HTTP请求并解析返回的JSON格式数据
response = requests.get(url + str(page), headers=headers)
data = json.loads(response.text)
# 判断评论总数是否已获取
if total_count == 0:
total_count = data['data']['page']['count']
# 判断是否存在评论
if 'data' in data:
reply_list = data['data']['replies']
# 如果存在评论,则将评论写入文件
with open(f'comment_files/{title}.csv', 'a', encoding='utf-8') as f:
for reply in reply_list:
f.write(reply['content']['message'] + '\n')