add:comments

This commit is contained in:
Sheyiyuan 2025-03-24 07:35:33 +08:00
parent a040da8242
commit af9887a0a6

51
main.py
View File

@ -206,24 +206,24 @@ class BiliWebCrawler:
print(f"解析UP主数据失败: {str(e)}")
send_notification(f"解析UP主数据失败: {str(e)}")
return None
try:
# 获取投稿列表
archive_url = f'https://api.bilibili.com/x/space/arc/search?mid={mid}&ps=30'
sleep(5)
archive_resp = self._safe_request(archive_url)
if archive_resp and archive_resp.status_code == 200:
archive_data = archive_resp.json()
# print(archive_data)
videos = archive_data.get('data', {}).get('list', {}).get('vlist', [])
# 计算30天前的时间戳
month_ago = time.time() - 30 * 86400
# 统计符合时间条件的视频
recent_count = sum(1 for v in videos if v.get('created') > month_ago)
up_info['近一个月投稿数'] = recent_count
except Exception as e:
print(f"获取投稿数据失败: {str(e)}")
send_notification(f"获取投稿数据失败: {str(e)}")
# try:
# # 获取投稿列表
# archive_url = f'https://api.bilibili.com/x/space/arc/search?mid={mid}&ps=30'
# sleep(1)
# archive_resp = self._safe_request(archive_url)
# if archive_resp and archive_resp.status_code == 200:
# archive_data = archive_resp.json()
# # print(archive_data)
# videos = archive_data.get('data', {}).get('list', {}).get('vlist', [])
#
# # 计算30天前的时间戳
# month_ago = time.time() - 30 * 86400
# # 统计符合时间条件的视频
# recent_count = sum(1 for v in videos if v.get('created') > month_ago)
# up_info['近一个月投稿数'] = recent_count
# except Exception as e:
# print(f"获取投稿数据失败: {str(e)}")
# send_notification(f"获取投稿数据失败: {str(e)}")
return up_info
@ -300,7 +300,7 @@ class BiliWebCrawler:
print(f"请求出错: {e}")
break
# 适当调整请求间隔
sleep(5)
sleep(1)
return comments
# 移除 fetch_comment_replies 方法(原方法不再需要)
@ -343,7 +343,7 @@ class BiliWebCrawler:
video_info_filename = os.path.join(video_dir, f'{self.bvid}_{play_count}_info.csv')
self.save_to_csv([video_info], video_info_filename)
sleep(5)
sleep(1)
# 新增弹幕抓取
print("正在抓取弹幕数据...")
@ -351,7 +351,7 @@ class BiliWebCrawler:
danmaku_filename = os.path.join(video_dir, f'{self.bvid}_{len(danmaku)}_danmaku.csv')
self.save_to_csv(danmaku, danmaku_filename)
sleep(5)
sleep(1)
# 新增评论抓取
print("正在抓取评论数据...")
@ -359,7 +359,7 @@ class BiliWebCrawler:
comments_filename = os.path.join(video_dir, f'{self.bvid}_{len(comments)}_comments.csv')
self.save_to_csv(comments, comments_filename)
sleep(5)
sleep(1)
# 新增UP主信息记录
print("正在获取UP主信息...")
@ -382,8 +382,13 @@ if __name__ == "__main__":
send_notification("未找到有效的BV号程序退出")
exit()
send_info_log(f"开始批量处理 {len(targets)} 个视频")
i=0
for bvid in targets:
print(f"\n{'=' * 30} 开始处理 {bvid} {'=' * 30}")
crawler = BiliWebCrawler(f"https://www.bilibili.com/video/{bvid}",cookie)
crawler.run()
sleep(5)
i=i+1
if i%20==0:
send_info_log(f"已完成 {i} 个视频,进度:{i/len(targets)*100:.2f}%")
sleep(1)
send_info_log(f"批量处理完成!")