import requests
from lxml import etree
def mkdir(path):
import os
path = path.strip()
path = path.rstrip('\\')
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print(path + '创建成功')
return True
else:
print('目录已存在')
return False
def get_cid():
url = 'http://www.leleketang.com/let3/knowledges.php?grade_id=30'
html = requests.get(url).text
e = etree.HTML(html)
global folders
folders = e.xpath('//div[@class="cal_name"]/a[@data-step=0]/text()')[::-1]
for folder in folders:
mkdir(f'D:/乐乐课堂视频/{folder}')
return e.xpath('//div[@class="cal_name"]/a[@data-step=0]/@data-cid')[::-1]
def download(url, fp, name):
res = requests.get(url).content
with open(fp + f'/{name}.mp4', 'wb') as f:
f.write(res)
if __name__ == '__main__':
max_page = 0
for k, cid in enumerate(get_cid()):
p = 1
while 1:
url = f'http://www.leleketang.com/let3/knowledge_list.php'
params = {
'cid': cid,
'p': p,
}
res = requests.get(url, params=params).text
e = etree.HTML(res)
if p == 1:
page_list = e.xpath('//a[@class="p_normal"]/text()')
if page_list:
max_page = int(page_list[-1])
# 每一页视频名字、链接列表
names = e.xpath('//div[@class="knowledge_info"]/div/text()')
urls = e.xpath('//div[@class="knowledge_btns"]/div/@data-video')
for j, url in enumerate(urls):
download(url, f'D:/乐乐课堂视频/{folders[k]}', names[j])
print(f'{names[j]} 已下载完成!')
print('-------------------------------------')
print(f'第{p}页下载完成!\n')
print('-------------------------------------')
p += 1
if not max_page or p > max_page:
break
网友评论