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)