From b8c9cad23d4824ca303cf276d3ba36a18affad86 Mon Sep 17 00:00:00 2001 From: p6fxi93qh <1240380517@qq.com> Date: Wed, 18 Sep 2024 19:36:28 +0800 Subject: [PATCH] =?UTF-8?q?task1=E5=8A=A0=E9=80=9F=E7=89=88=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E5=88=A9=E7=94=A8=E5=A4=9A=E7=BA=BF=E7=A8=8B=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=92=88=E5=AF=B9=E8=80=97=E6=97=B6=E9=95=BF=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=87=BD=E6=95=B0=E8=BF=9B=E8=A1=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task1_2.py | 87 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/task1_2.py b/task1_2.py index 9b0d8f2..6b6b3f1 100644 --- a/task1_2.py +++ b/task1_2.py @@ -1,30 +1,57 @@ - -from concurrent.futures import ThreadPoolExecutor -import os -import threading -from getdm import get_videos_url,get_danmu - -def write_dm_to_file(video_id, file, lock): - danmu = get_danmu(video_id) - with lock: - for item in danmu: - file.write(f"{item}\n") # 每个弹幕单独一行 - print(f"视频 {video_id} 的弹幕已写入") - -def writedmtxt(file_path, res): - with open(file_path, 'w', encoding='utf-8') as file: - lock = threading.Lock() - with ThreadPoolExecutor(max_workers=10) as executor: - futures = [executor.submit(write_dm_to_file, video_id, file, lock) for video_id in res] - for future in futures: - future.result() # 获取函数结果,这里可以用来处理异常 - -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 +import os +import threading +from getdm import get_videos_url, get_danmu, get_web_url + +# 定义全局锁 +lock = threading.Lock() + +def getalldm(videos_url, savepath): + with open(savepath, 'w', encoding='utf-8') as file: + for item in videos_url: + res = get_danmu(item) + with lock: # 确保写入操作是互斥的 + for result in res: + file.write(f"{result}\n ") + file.write("\n") + +def get_danmu_thread(item, savepath): + res = get_danmu(item) + with lock: + with open(savepath, 'a', encoding='utf-8') as file: # 以追加模式打开文件 + for result in res: + file.write(f"{result}\n ") + file.write("\n") + +def main(): + # 获取所有页面的 URL + pages = 10 + keyword = '2024巴黎奥运会' + webs_url = get_web_url(keyword, pages) + + # 获取所有视频的 URL + videos_url = [] + for item in webs_url: + now = get_videos_url(item) + videos_url.extend(now) + + # 定义保存弹幕的文件路径 + res_dir = os.path.join(os.getcwd(), 'res') + if not os.path.exists(res_dir): + os.makedirs(res_dir) + file_name = "total_final_fast.txt" + savepath = os.path.join(res_dir, file_name) + + # 创建并启动线程 + threads = [] + for item in videos_url: + thread = threading.Thread(target=get_danmu_thread, args=(item, savepath)) + threads.append(thread) + thread.start() + + # 等待所有线程完成 + for thread in threads: + thread.join() + print("所有视频弹幕已处理完成。") + +if __name__ == '__main__': + main() \ No newline at end of file