From aad2ee850243f9ee55d3726b21205aa9e79d7aca Mon Sep 17 00:00:00 2001 From: p6fxi93qh <1240380517@qq.com> Date: Wed, 18 Sep 2024 23:12:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=95=B0=E6=8D=AE=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E9=83=A8=E5=88=86=E8=BF=9B=E8=A1=8C=E6=80=A7=E8=83=BD=E5=88=86?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task1_profile.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 task1_profile.py diff --git a/task1_profile.py b/task1_profile.py new file mode 100644 index 0000000..64306d8 --- /dev/null +++ b/task1_profile.py @@ -0,0 +1,35 @@ +import cProfile +import os +from getdm import get_videos_url,get_danmu,get_web_url + +def getalldm(videos_url,savepath): #从所有视频中获得弹幕 + with open(savepath, 'w', encoding='utf-8') as file: # 打开文件准备写入 + for item in videos_url : + res = get_danmu(item) + for result in res : + file.write(f"{result}\n ") + file.write("\n") + +def profile_getalldm(): + # 获取所有页面的 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.txt" + savepath = os.path.join(res_dir, file_name) + os.makedirs(os.path.dirname(savepath), exist_ok=True) + getalldm(videos_url, savepath) + +if __name__ == '__main__': + cProfile.run('profile_getalldm()', 'profile_results.txt') +