From c3f128c9eb79ec3add3a4a2704edcddc3a6881fe Mon Sep 17 00:00:00 2001 From: p6fxi93qh <1240380517@qq.com> Date: Tue, 17 Sep 2024 23:24:24 +0800 Subject: [PATCH] Update task1.py --- task1.py | 78 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/task1.py b/task1.py index 486af20..aec9cf8 100644 --- a/task1.py +++ b/task1.py @@ -1,39 +1,41 @@ -#收集综合排名前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秒 - - - - - - +import requests +import sys +import os +import re +import time +from getdm import get_videos_url,get_danmu + + +def writedmtxt(file_path,res): #将300个视频弹幕写入txt文件中 + 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秒 + + +if __name__ == '__main__' : + 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) + file_name = "total300_3.txt" + file_path = os.path.join(res_dir, file_name) + writedmtxt(file_path=file_path,res=res) + + + + + + \ No newline at end of file