Skip to content

Commit 8c35e5a

Browse files
authored
Create douyin_pro_2.py
1 parent ecfae84 commit 8c35e5a

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

douyin_pro_2.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# -*- coding:utf-8 -*-
2+
from splinter.driver.webdriver.chrome import Options, Chrome
3+
from splinter.browser import Browser
4+
from contextlib import closing
5+
import requests, json, time, re, os, sys, time
6+
from bs4 import BeautifulSoup
7+
8+
class DouYin(object):
9+
def __init__(self, width = 500, height = 300):
10+
"""
11+
抖音App视频下载
12+
"""
13+
pass
14+
15+
def get_video_urls(self, user_id):
16+
"""
17+
获得视频播放地址
18+
Parameters:
19+
user_id:查询的用户ID
20+
Returns:
21+
video_names: 视频名字列表
22+
video_urls: 视频链接列表
23+
nickname: 用户昵称
24+
"""
25+
video_names = []
26+
video_urls = []
27+
unique_id = ''
28+
while unique_id != user_id:
29+
search_url = 'https://api.amemv.com/aweme/v1/discover/search/?cursor=0&keyword=%s&count=10&type=1&retry_type=no_retry&iid=17900846586&device_id=34692364855&ac=wifi&channel=xiaomi&aid=1128&app_name=aweme&version_code=162&version_name=1.6.2&device_platform=android&ssmix=a&device_type=MI+5&device_brand=Xiaomi&os_api=24&os_version=7.0&uuid=861945034132187&openudid=dc451556fc0eeadb&manifest_version_code=162&resolution=1080*1920&dpi=480&update_version_code=1622' % user_id
30+
req = requests.get(url = search_url, verify = False)
31+
html = json.loads(req.text)
32+
aweme_count = html['user_list'][0]['user_info']['aweme_count']
33+
uid = html['user_list'][0]['user_info']['uid']
34+
nickname = html['user_list'][0]['user_info']['nickname']
35+
unique_id = html['user_list'][0]['user_info']['unique_id']
36+
user_url = 'https://www.douyin.com/aweme/v1/aweme/post/?user_id=%s&max_cursor=0&count=%s' % (uid, aweme_count)
37+
req = requests.get(url = user_url, verify = False)
38+
html = json.loads(req.text)
39+
i = 1
40+
for each in html['aweme_list']:
41+
share_desc = each['share_info']['share_desc']
42+
if '抖音-原创音乐短视频社区' == share_desc:
43+
video_names.append(str(i) + '.mp4')
44+
i += 1
45+
else:
46+
video_names.append(share_desc + '.mp4')
47+
video_urls.append(each['share_info']['share_url'])
48+
49+
return video_names, video_urls, nickname
50+
51+
def get_download_url(self, video_url, watermark_flag):
52+
"""
53+
获得带水印的视频播放地址
54+
Parameters:
55+
video_url:带水印的视频播放地址
56+
Returns:
57+
download_url: 带水印的视频下载地址
58+
"""
59+
req = requests.get(url = video_url, verify = False)
60+
bf = BeautifulSoup(req.text, 'lxml')
61+
script = bf.find_all('script')[-1]
62+
video_url_js = re.findall('var data = \[(.+)\];', str(script))[0]
63+
video_html = json.loads(video_url_js)
64+
# 带水印视频
65+
if watermark_flag == True:
66+
download_url = video_html['video']['play_addr']['url_list'][0]
67+
# 无水印视频
68+
else:
69+
download_url = video_html['video']['play_addr']['url_list'][0].replace('playwm','play')
70+
return download_url
71+
72+
def video_downloader(self, video_url, video_name, watermark_flag=False):
73+
"""
74+
视频下载
75+
Parameters:
76+
video_url: 带水印的视频地址
77+
video_name: 视频名
78+
watermark_flag: 是否下载带水印的视频
79+
Returns:
80+
81+
"""
82+
size = 0
83+
video_url = self.get_download_url(video_url, watermark_flag=watermark_flag)
84+
with closing(requests.get(video_url, stream=True, verify = False)) as response:
85+
chunk_size = 1024
86+
content_size = int(response.headers['content-length'])
87+
if response.status_code == 200:
88+
sys.stdout.write(' [文件大小]:%0.2f MB\n' % (content_size / chunk_size / 1024))
89+
90+
with open(video_name, "wb") as file:
91+
for data in response.iter_content(chunk_size = chunk_size):
92+
file.write(data)
93+
size += len(data)
94+
file.flush()
95+
96+
sys.stdout.write(' [下载进度]:%.2f%%' % float(size / content_size * 100) + '\r')
97+
sys.stdout.flush()
98+
99+
def run(self):
100+
"""
101+
运行函数
102+
Parameters:
103+
None
104+
Returns:
105+
None
106+
"""
107+
self.hello()
108+
user_id = input('请输入ID(例如145651081):')
109+
watermark_flag = int(input('是否下载带水印的视频(0-否,1-是):'))
110+
video_names, video_urls, nickname = self.get_video_urls(user_id)
111+
if nickname not in os.listdir():
112+
os.mkdir(nickname)
113+
print('视频下载中:共有%d个作品!\n' % len(video_urls))
114+
for num in range(len(video_urls)):
115+
print(' 解析第%d个视频链接 [%s] 中,请稍后!\n' % (num+1, video_urls[num]))
116+
if '\\' in video_names[num]:
117+
video_name = video_names[num].replace('\\', '')
118+
elif '/' in video_names[num]:
119+
video_name = video_names[num].replace('/', '')
120+
else:
121+
video_name = video_names[num]
122+
self.video_downloader(video_urls[num], os.path.join(nickname, video_name), watermark_flag)
123+
print('\n')
124+
125+
print('下载完成!')
126+
127+
def hello(self):
128+
"""
129+
打印欢迎界面
130+
Parameters:
131+
None
132+
Returns:
133+
None
134+
"""
135+
print('*' * 100)
136+
print('\t\t\t\t抖音App视频下载小助手')
137+
print('\t\t作者:Jack Cui')
138+
print('*' * 100)
139+
140+
141+
if __name__ == '__main__':
142+
douyin = DouYin()
143+
douyin.run()

0 commit comments

Comments
 (0)