This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
douyin_all.py
69 lines (61 loc) · 2.13 KB
/
douyin_all.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/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)
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'
}
response = requests.get(
url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids='+str(vid),
headers = headers
)
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)
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("输入有误!")