import requests import re import json import os class douyin_video(): def __init__(self): self.headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch, br', 'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Mobile Safari/537.36' } self.url = '' def get_video_info(self): video_id = re.search('video/.*?/', get_local_url(headers=self.headers, url=self.url)).group().replace('video/', '').replace('/', '') url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' + video_id res = requests.get(url=url) data = json.loads(res.content)['item_list'][0] return { 'file_name': validateTitle(data['desc']), 'vid': data['video']['vid'] } def down_video(self, name_vidObj): url = 'https://aweme.snssdk.com/aweme/v1/play/?video_id=%s&ratio=720p&line=0' % name_vidObj['vid'] video_url = get_local_url(url=url, headers=self.headers) res = requests.get(url=video_url) with open(name_vidObj['file_name'] + '.mp4', 'wb') as f: f.write(res.content) print('%s 下载完成!' % name_vidObj['file_name']) def get_local_url(url, headers): html = requests.get(url, headers=headers, allow_redirects=False) return html.headers['Location'] def validateTitle(title): rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? " < > |' new_title = re.sub(rstr, "_", title) # 替换为下划线 return new_title if __name__ == '__main__': getVideo = douyin_video() while True: while True: url = input('输入抖音url短链: ') if url != '': break getVideo.url = url try: getVideo.down_video(getVideo.get_video_info()) print('\n') except: print('错误的分享链接,或该接口失效\n') input('按任意键重试\n') os.system('cls')
打开之后按提示输入抖音视频的url短链,格式为 https://v.douyin.com/XXXXXXX/
短链在APP端点分享,里面有链接分享,那个分享链接就是抖音短链
按回车即可下载到当前目录下。文件名按照抖音视频名命名:
编译后exe可执行文件:(用pyinstall打包的)