import os from getdm import get_videos_url,get_danmu,get_web_url headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0"} cookies = " buvid3=8231EFA3-C943-B46F-C28D-10CA6EB40DD180949infoc; CURRENT_FNVAL=4048; rpdid=|(JYYJ|u)luu0J'uYmkl~mY)k; buvid4=CA2A9538-3AB7-5FF0-1AD2-830935987DFB07953-024012212-75GkjfNGeaGAWMbBOvqWtQ%3D%3D; buvid_fp=f29e4e3ef42f7eb5eeb379115e870ad9; LIVE_BUVID=AUTO7217086875647310; PVID=2; b_nut=100; _uuid=7FDC1091C-5D99-24A8-4133-F89DFB510EA6531786infoc; header_theme_version=CLOSE; enable_web_push=DISABLE; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjY2NDk2NTcsImlhdCI6MTcyNjM5MDM5NywicGx0IjotMX0.3Z1vsF3Xzu2nPlPrZBCesXytvgQb_37-XNcBtrRlMzE; bili_ticket_expires=1726649597; bsource=search_bing; bmg_af_switch=1; bmg_src_def_domain=i1.hdslb.com; home_feed_column=4; SESSDATA=188e2907%2C1742055275%2C569b4%2A92CjDCOHUaxUkq_L5c6PfUab0-sYNi5nOwFET1z4H9q14EuO2HVsnxLmx1932gpvUQBgESVnFIY3lzUFJhZWxtY1N1d21POEc5bDRKRldVTlJSTDNWdG8ybkJSYmdGN3g3S2hjRk1uYzhSVEJUUkVOMThzb2Zfb0txZlUwbDNVdnZEM1h6UjJ6amhBIIEC; bili_jct=b1a79048046b927643ac32fe2ce260a0; DedeUserID=1986103749; DedeUserID__ckMd5=be6d80b5f24d01e0; browser_resolution=1220-150; sid=6jgymz8o; bp_t_offset_1986103749=977826403892330496; b_lsid=EF11D7E6_191FBE2F27F" dic_cookies = {} for i in cookies.split("; "): dic_cookies[i.split("=")[0]] = i.split("=")[1] 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") if __name__ == '__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 = videos_url + 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)