Update task1.py

main
p6fxi93qh 5 months ago
parent fce829a4e4
commit c3f128c9eb

@ -1,5 +1,3 @@
#收集综合排名前300的视频弹幕
#from geturl import GetVideoURL
import requests import requests
import sys import sys
import os import os
@ -7,29 +5,33 @@ import re
import time import time
from getdm import get_videos_url,get_danmu from getdm import get_videos_url,get_danmu
keyword = '2024巴黎奥运会'
res = get_videos_url(keyword,10) def writedmtxt(file_path,res): #将300个视频弹幕写入txt文件中
count = 0
res_dir = os.path.join(os.getcwd(), 'res') #连续写入
if not os.path.exists(res_dir): with open(file_path, 'w', encoding='utf-8') as file: # 打开文件准备写入
os.makedirs(res_dir) for video_id in res:
danmu = get_danmu(video_id)
# 指定输出文件"total300.txt" # 写入这是第几个视频的弹幕
file_name = "total300_3.txt" count += 1
file_path = os.path.join(res_dir, file_name) file.write(f"这是第{count}个视频的弹幕===================:\n")
count = 0 for item in danmu:
#连续写入 file.write(f"{item} ")
with open(file_path, 'w', encoding='utf-8') as file: # 打开文件准备写入 file.write("\n")
for video_id in res: print(f"{count}个视频的弹幕已写入")
danmu = get_danmu(video_id) time.sleep(5) # 等待6秒
# 写入这是第几个视频的弹幕
count += 1
#file.write(f"这是第{count}个视频的弹幕===================:\n") if __name__ == '__main__' :
for item in danmu: keyword = '2024巴黎奥运会'
file.write(f"{item} ") res = get_videos_url(keyword,10)
file.write("\n") #指定输出位置
print(f"{count}个视频的弹幕已写入") res_dir = os.path.join(os.getcwd(), 'res')
time.sleep(5) # 等待6秒 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)

Loading…
Cancel
Save