1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
import os import requests
from time import sleep from multiprocessing import Pool
def mkdir(path): isExists = os.path.exists(path) if not isExists: os.makedirs(path) print('文件夹创建成功') else: print('文件夹已存在')
def forNum(n): n = int(n) if n < 10: a = '00000' + str(n) else: if n < 100: a = '0000' + str(n) else: if n < 1000: a = '000' + str(n) else: if n < 10000: a = '00' + str(n) else: if n < 100000: a = '0' + str(n) else: if n < 1000000: a = str(n) return a
def editM3u8(url_m3u8): f = open(url_m3u8, 'r', encoding='utf-8') text_list = f.readlines() files = [] for i in text_list: if i.find('#EX') == -1: files.append(str.strip(i)) f.close() return files
def download_init(files, tit_link): if __name__ == "__main__": pool = Pool(4) for n in range(0, len(files)): a = forNum(n) url = tit_link + str(files[n]) pool.apply_async(download, (url, a)) pool.close() pool.join()
def download(url, num): headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" } print(url) response = requests.get(url, headers=headers) f = open("./ts/" + num + '.ts', "wb") f.write(response.content) f.close() print(num + "下载完成")
def delFile(file): os.remove(file)
url_m3u8 = './ts/boss.m3u8' tit_link = "https://baikevideo.cdn.bcebos.com/media/mda-XzWxczOmPXU0VoI4/" mkdir('./ts/')
files = editM3u8(url_m3u8)
download_init(files, tit_link)
|