爬取钉钉视频
免责声明
此脚本仅供学习参考,切勿违法使用下载他人资源进行售卖,本人不但任何责任!
仓库地址:
执行顺序
- poxyM3u8开启代理
- getM3u8url用于获取m3u8文件
- userAgent随机请求头
- downVideo|downVideoThreadTqdm单线程下载和多线程下载,二选一即可
启动顺序:poxyM3u8开启代理 -> getM3u8url获取文件->downVideo遍历文件进行下载
像这样别人给的钉钉链接我想要它的视频, 但是又没有下载按钮,我该怎么办呢?
我想到了用爬虫爬取
方案一
检查了一下网络请求发现它是采用m3u8文件格式保存的,所以找m3u8的文件。
找到了
-
对它写代码进行保存:
with open("4f8122f4-f8fb-43d5-b8c8-7c1c9a4a70f7_normal.m3u8", "r", encoding="utf-8") as f: centen = f.read() print(centen) pattern = r'([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\/[\d]+\.ts\?auth_key=[\d\w-]+)' matches = re.findall(pattern, centen) print(matches) # urls = [] for match in matches: url = "https://dtliving-bj.dingtalk.com/live_hp/" + match urls.append(url) # print(len(urls)) # for i in urls: # print(i) for item in tqdm(urls,disable="下载"): response = requests.get(item) with open("E:/a.mp4", "ab", ) as f: f.write(response.content)
下载是下载下来了, 可是我有很多很多集,我自己下载是不是太麻烦了,也累。所以我就分析了一下这个地址
发现:
m3u8: https://dtliving-sz.dingtalk.com/live_hp/8618428f-dc2e-419e-bc6b-b93a6ee6b28c_normal.m3u8?auth_key=1730544823-fb9347e4a68a456b8b265afa36700f15-0-f24f0b45c72dd6547dadf77466f68ce4 url: https://n.dingtalk.com/dingding/live-room/index.html?roomId=ZxaInSr3io8j9iZf&liveUuid=8618428f-dc2e-419e-bc6b-b93a6ee6b28c 8618428f-dc2e-419e-bc6b-b93a6ee6b28c_normal.m3u8,其中8618428f-dc2e-419e-bc6b-b93a6ee6b28c是Uuid
既然:
8618428f-dc2e-419e-bc6b-b93a6ee6b28c
是房间号的话那我把好多集的房间号爬下来然后拼接到dtliving-sz.dingtalk.com/live_hp/房间号_normal.m3u8这样不就行了?然后拼接好我就发了一个请求发现并不能下载下来
原因是`auth_key`的原因, 然后我尝试寻找`auth_key`
emmm, 找了许久,打扰了。还是能力不够, 所以打算换一个方式。
方案二
我发现浏览器是可以获取到auth_key的那我不如我去拿浏览器的响应值。
相当于做了一件中间人的方式把我想要的东西抓取出来。
我使用了mitmproxy
当我的代理
pip install mitmproxy
然后写一段代码来捕捉我想要抓取的url的响应
from mitmproxy import ctx,http
# http://mitm.it/ 证书
# mitmdump.exe -s .\test5.py
# mitmweb
import re
import requests
def request(flow):
# 获取请求对象
request = flow.request
# 实例化输出类
math = re.match("^(.*?)_normal.m3u8", request.url)
if math:
info = ctx.log.info
# 打印请求的url
info("请求地址: " + request.url + "\n")
string = request.url
start_index = string.find("auth_key=") + len("auth_key=")
end_index = len(string)
result = string[start_index:end_index]
print(result)
info("请求体: " + request.text + "\n")
# # 打印请求方法
info("请求方法: " + request.method)
def response(flow):
m3u8math = re.match("^(.*?)_normal.m3u8", flow.request.url)
if m3u8math:
print("===============这是m3u8格式的文件响应============================")
centen = flow.response.get_text()
with open("./m3u8s/{0}.m3u8".format(title), "w") as f:
f.write(centen)
print("===============结束============================")
代码写好了,然后打开本机代理改成mitmproxy的代理然后安装证书,之后就可以愉快的抓请求了
1、代码启动
2、代理设置:
3、证书安装:
- 设置好系统代理后,浏览器输入
http://mitm.it/
, 然后选择对应系统的证书安装就行。
4、抓取
- 当我使用浏览器打开
https://n.dingtalk.com/dingding/live-room/index.html?roomId=AAToXdFAVGArvaQx&liveUuid=9aac3549-698f-46b9-9bb0-f2f44d4faaca
的时候它就会帮我把特定m3u8的请求响应做文件保存
from mitmproxy import ctx,http
# http://mitm.it/ 证书
# mitmdump.exe -s .\xiaoyuan.py
# mitmweb
import re
import requests
def response(flow):
titlesearch = re.search(r"roomId=(.*?)&liveUuid=(.*)", flow.request.url)
if titlesearch:
global roomIdAndUid
roomIdAndUid = titlesearch
centent = flow.response.get_content().decode('utf-8')
titleRe = re.search(r'<meta property="og:title" content="(.*?)">',centent)
global title
title = titleRe.group(1)
print(title)
else:
m3u8math = re.match(r"^(.*)/(.*?)_normal.m3u8", flow.request.url)
if m3u8math:
print("===============这是m3u8格式的文件响应============================")
print("房间号:", roomIdAndUid.group(2), "========", roomIdAndUid.group(1))
centen = flow.response.get_text()
try:
with open("./杰哥数学m3u8/{0}.m3u8".format(title), "w") as f:
f.write(centen)
except OSError:
with open("./log.txt".format(title), "a") as f:
f.write("标题: {0}, roomId:{1}, UuId: {2}, url:https://n.dingtalk.com/dingding/live-room/index.html?roomId={3}&liveUuid={4}\n".
format(title,
roomIdAndUid.group(1),
roomIdAndUid.group(2),
roomIdAndUid.group(1)