python语言tdcq剧集下载爬虫程序代码

import requests
import re
import os
from lxml import etree

url = “https://xunaizhan.com/xgplay/tiandichuanqi-1-1/”

h = {‘user-agent’:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0’
}
r1 = requests.get(url=url, headers=h)

print(r1.text)

d = re.findall(r’\/(.*?.\)',r1.text)[4]

pattern = re.compile(r"[/\?“<>|]”) # ‘/ \ : * ? " < > |’
d = re.sub(pattern, “”, d) # 替换为下划线

print(d)

#https://vip.lz-cdn16.com/20230323/14124_6a61dba8/2000k/hls/mixed.m3u8
m = f’https://vip.lz-cdn16.com/20230323/{d}/2000k/hls/mixed.m3u8’

print(m)

r2 = requests.get(url=m, headers=h)

print(r2.text)

创建一个文件夹用于存放视频片段,取名为"sp"

if not os.path.exists(“ts”):
os.mkdir(“ts”)
num = 0
data = re.findall(r’,\n(.*?.ts)', r2.text, re.S)

print(data)

#https://vip.lz-cdn16.com/20230323/14124_6a61dba8/2000k/hls/a95464ecb88000001.ts
for ts in data:
#print(ts)

ts = f'https://vip.lz-cdn16.com/20230323/{d}/2000k/hls/' + ts
# print(ts)
res3 = requests.get(ts, headers=h)
with open(f'ts/{num}.ts', 'wb') as f:
    f.write(res3.content)
    print(f"{num}.ts文件保存完成!")
num += 1

你可能感兴趣的:(python,爬虫,开发语言)