task1获取300个视频的弹幕(更新)

main
p6fxi93qh 2 months ago
parent f3619bf4a9
commit 6a161daaec

@ -1,39 +1,39 @@
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秒
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巴黎奥运会'
res = get_videos_url(keyword,10)
#指定输出位置
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 = "total300_3.txt"
file_path = os.path.join(res_dir, file_name)
writedmtxt(file_path=file_path,res=res)
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)

Loading…
Cancel
Save