# 版本介绍

1.Python源文件版本:支持不去除中文等多余字符、支持下载视频和背景音乐

2.Django+HTML网页版本:支持不去除多余中文、支持下载视频、支持接口请求、不支持背景音乐

网站为 「 https://dy.nisekoo.com

接口为 「 https://dy.nisekoo.com/api 」仅支持GET请求,参数只要提交URL即可,可以不去除中文等多余字符

# 声明

所有视频与图片版权归属「字节跳动」,本站不存储任何视频与图片。

# 原理

1.模拟UA,去访问抖音官方提供的链接

2.得到重定向之后的链接,提取视频的ID

3.使用ID,向抖音官方接口发送POST请求

4.返回的JSON,提取有用的Value

# 源代码

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# https://github.com/missuo/douyin


import requests
import re
import webbrowser

# 用户输入抖音
all_url = input("请输入需要解析的链接:") 
pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')    # 匹配模式
url_list = re.findall(pattern,all_url) 
url = url_list[0]
#print(url)

def get_redirect_url(url):
	header = {
		'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
		'Upgrade-Insecure-Requests': '1',
	}
	data = requests.get(headers=header, url=url, timeout=5)
	vid = re.findall(r'\d+',data.url)
	return vid[0]
vid = get_redirect_url(url)
#print(vid)

response = requests.get('https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids='+str(vid))
item = response.json().get("item_list")[0]
#print(item)

mp4 = item.get("video").get("play_addr").get("url_list")[0].replace("playwm", "play")
print('真实的视频链接为:',mp4)
mp3url = item.get("music").get("play_url").get("url_list")[0]
print('真实的音乐链接为:',mp3url)
headers = {
	'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
	'Upgrade-Insecure-Requests': '1',
}
res = requests.get(mp4, headers=headers, allow_redirects=True)
mp4url = res.url

desc = item.get("desc")
video = requests.get(url=mp4url, headers=headers)
audio = requests.get(url=mp3url, headers=headers)
flag = int(input(("请选择你要下载的类型:1.视频 2.音乐 3.全部"+"\n")))
if flag == 1:
	with open(desc+".mp4", 'wb') as f:
		f.write(video.content)
		f.close()
		print(u"视频已经完成下载。")
elif flag == 2:
	with open(desc+".mp3", 'wb') as f2:
		f2.write(audio.content)
		f2.close()
		print(u"音乐已经完成下载。")
elif flag == 3:
	with open(desc+".mp4", 'wb') as f3, open(desc+".mp3", 'wb') as f4:
		f3.write(video.content)
		f3.close()
		f4.write(audio.content)
		f4.close()
		print("视频和音乐完成下载")
else:
	print("输入有误!")