[Python] 虎课网批量下载
本帖最后由 宇文卜吉格 于 2020-10-28 09:01 编辑前言
前几天我发现虎课网改版了,不能直接用猫抓抓m3u8链接,然后到m3u8下载器下载了,所以今天研究了下,顺便写了个可以批量下载的小程序。
代码import requests
from lxml import etree
import re
import os
headers_mobile = {
'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
}
Lists = []
# 实现单个视频下载
def single_run(url2,Cookie,_csrf_frontend):
url1 = 'https://m.huke88.com/video/video-url'
id = re.findall(r'\b\d+\b', url2)
headers1 = {
'Cookie':Cookie,
'Host': 'm.huke88.com',
'Referer': url2,
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Origin': 'https://m.huke88.com',
'X-Requested-With': 'XMLHttpRequest',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin'
}
data = {
'id': id,
'_csrf-frontend': _csrf_frontend
}
response = requests.post(url=url1, headers=headers1, data=data)
videoUrl = response.json().get('data').get('videoUrl')
response = requests.get(url=url2, headers=headers_mobile)
html = etree.HTML(response.text)
title = html.xpath("//h1[@class='con']/text()")
infos = {
'videoUrl':videoUrl,
'title':title
}
print(infos)
Lists.append(infos)
def Parse_page(url,Cookie):
response = requests.get(url=url)
html = etree.HTML(response.text)
#First_dir = html.xpath("//h2/text()")
section_lists = html.xpath("//div[@class='item-tit']")
for les_item in section_lists:
hrefs = les_item.xpath("//div[@class='cont-box']/div[@class='box-main']//a[@target='_blank']/@href")
for href in hrefs:
if href != "https:/":
href = "https://huke88.com" + href
single_run(href,Cookie)
# 只做解析,生成 bat 文件,用 nilaoda 的下载器下载即可
def write(List):
with open('格式.bat','w',encoding='utf-8') as f:
f.write('@echo off'+'\n'+'::Created by N_m3u8DL-CLI-SimpleG'+ '\n' +r'chcp 65001 >nul'+'\n')
dir = r'./Downloads'
if os.path.exists(dir) == False:
os.makedirs('Downloads')
for i in List:
try:
f.write('"' + "N_m3u8DL-CLI_v2.7.5.exe" + '"' + ' ' + '"' + i.get('videoUrl') + '"' + ' ' + '--workDir' + ' ' + dir + ' ' + '--saveName' + ' ' + i.get('title') + '\n')
except:
pass
if __name__ == '__main__':
print('请输入网址,例如:https://huke88.com/route/ae.html 或 https://huke88.com/course/3238.html')
url = input('请输入网址:')
#Cookie 需要改 UA 再获取
Cookie = input('请输入Cookie:')
# video-play 里面可以看到
_csrf_frontend = input('请输入_csrf-frontend:')
a = url.split('/')
if a[-2] == 'route':
Parse_page(url, Cookie)
write(Lists)
else:
single_run(url, Cookie=Cookie,_csrf_frontend=_csrf_frontend)
write(Lists)
用法
程序需要输入三个东西
1.是网址,这里 https://huke88.com/route/ps.html 就是批量,https://huke88.com/course/2018.html ,就是单个视频下载。
2、3 都需要改 UA 刷新网页在 video-url 中获取
视频教程+软件:https://aohua.lanzoui.com/iBN6nhskrxa
吐槽
虎课网就算改版了,还是可以从上面下载视频。至于为什么非要改 UA 才能下载视频,别问我为什么,这是经验。
还有代码是今天晚上加紧写出来的,质量也不高,能用就行了,如果没有什么特别大的问题就不更新了。
本程序只做解析,生成的 bat 文件请用 https://github.com/nilaoda/N_m3u8DL-CLI/releases 下载。
多谢分享多谢分享 谢谢楼主分享,先收藏了。 谢谢大佬 谢谢分享 6666
不错不错~~~~~ 多谢楼主分享
支持楼主,谢谢分享。 谢谢大佬分享
页:
[1]
2