parent
d06df68b5b
commit
b8c9cad23d
@ -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)
|
||||
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()
|
Loading…
Reference in new issue