From f84f95ec883b5780fcf643a3eae0a43c494ed724 Mon Sep 17 00:00:00 2001 From: p6fxi93qh <1240380517@qq.com> Date: Tue, 17 Sep 2024 16:26:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E9=9B=86=E7=BB=BC=E5=90=88=E6=8E=92?= =?UTF-8?q?=E5=90=8D=E5=89=8D300=E7=9A=84=E8=A7=86=E9=A2=91=E5=BC=B9?= =?UTF-8?q?=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task1.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 task1.py diff --git a/task1.py b/task1.py new file mode 100644 index 0000000..486af20 --- /dev/null +++ b/task1.py @@ -0,0 +1,39 @@ +#收集综合排名前300的视频弹幕 +#from geturl import GetVideoURL +import requests +import sys +import os +import re +import time +from getdm import get_videos_url,get_danmu + +keyword = '2024巴黎奥运会' +res = get_videos_url(keyword,10) + +res_dir = os.path.join(os.getcwd(), 'res') +if not os.path.exists(res_dir): + os.makedirs(res_dir) + +# 指定输出文件"total300.txt" +file_name = "total300_3.txt" +file_path = os.path.join(res_dir, file_name) +count = 0 +#连续写入 +with open(file_path, 'w', encoding='utf-8') as file: # 打开文件准备写入 + for video_id in res: + danmu = get_danmu(video_id) + # 写入这是第几个视频的弹幕 + count += 1 + #file.write(f"这是第{count}个视频的弹幕===================:\n") + for item in danmu: + file.write(f"{item} ") + file.write("\n") + print(f"第{count}个视频的弹幕已写入") + time.sleep(5) # 等待6秒 + + + + + + + \ No newline at end of file