python 下载 m3u8
时间: 2023-12-04 07:39:17 浏览: 128
Download the files of m3u8 using python. (基于Python的m3u8下载器).zip
以下是使用Python下载m3u8的示例代码:
```python
import requests
import os
import threading
def download(url, path):
r = requests.get(url, stream=True)
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
def download_m3u8(url, path):
r = requests.get(url)
if r.ok:
if not os.path.exists(path):
os.makedirs(path)
ts_list = [url.rsplit("/", 1)[0] + "/" + i for i in r.text.split("\n") if i.endswith(".ts")]
urlheader = url.rsplit("/", 1)[0] + "/"
for i in ts_list:
if "#" not in i:
i = i.replace("\n","")
n = i[-7:]
threading.Thread(target=download, args=(urlheader+""+i,path+"/"+str(n),)).start()
# 示例
download_m3u8("http://example.com/example.m3u8", "example")
```
该示例代码使用requests库下载m3u8文件,并使用多线程下载ts文件,提高下载速度。你可以将示例代码中的URL和路径替换为你自己的URL和路径。
阅读全文