import requests
from concurrent.futures import ThreadPoolExecutor
myHeasers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
}
#获取全部链接列表
def getUrlList():
res = requests.get("https://pptv.1080tg.com/202306/09/WyBkvykgLm3/video/900k_0X480_64k_25/hls/index.m3u8")
tsList = res.text.split("\n")
urlList = [];
for i in tsList:
if i.startswith("#"):
continue
urlList.append(i)
return urlList
#下载内容
def download_file(url,urls,i):
if isinstance(url, str):
print("下载中"+url)
res = requests.get(url,headers=myHeasers)
urls[i] = res.content
#检验
def checkCotent(urls):
i = 0
for url in urls:
if isinstance(url, bytes):
pass
else:
i += 1
return i
def downStart(urls):
# 线程池,程序入口 max_workers=线程个数配置
with ThreadPoolExecutor(max_workers=20) as executor:
#print(urls)
# 提交任务
i = 0
for url in urls:
executor.submit(download_file, url, urls, i)
i += 1
# 等待完成
executor.shutdown(wait=True)
count = checkCotent(urls)
print(count)
#递归函数
if count > 0:
print("有"+str(count)+"个文件没有下载成功,重新开启线程下载中")
downStart(urls)
else:
print("文件生成中")
for line in urls:
with open("movies.mp4","ab+") as f:
f.write(line)
urls = getUrlList()
downStart(urls)
最后修改:2023 年 10 月 02 日
© 允许规范转载