# encoding='utf-8'
import requests
import json
import re
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'cache-control': 'max-age=0',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
#'cookie':'_ga=GA1.2.1916058393.1585925390; ttwid=1%7CcB4K-RSNyLaXmUwzyMU4cQKqQjad-HMcyJbtLg03s00%7C1597896220%7Cfd5b5e20d168649c1260129c93eaa23bdc677a5253a38114f62a207875cc9e2a; passport_csrf_token=7285824b7e27a76ecee9d6d7bf77e63c; odin_tt=835e397d29269cd78661deff5f8a8b59e89432f2902f89b29e868ad582f78a6c35ea74dbe494c9f9d656778f8b9f8a7f; sso_uid_tt=de877924a7df7c228555dcb1e15fe83c; sso_uid_tt_ss=de877924a7df7c228555dcb1e15fe83c; toutiao_sso_user=1549da1d811fcb6f9c75107ae2fe5fe7; toutiao_sso_user_ss=1549da1d811fcb6f9c75107ae2fe5fe7; passport_auth_status=efc9e9fc48804e02e4de3c3947ec177c%2C9b4ca466759bb37d9a0dca2beb6e01d3; sid_guard=88e517f036ea9dff309e1c54d0a73d6d%7C1603722040%7C5184000%7CFri%2C+25-Dec-2020+14%3A20%3A40+GMT; uid_tt=8b913c22b66ff9be0e68ab46ccc3466e; uid_tt_ss=8b913c22b66ff9be0e68ab46ccc3466e; sid_tt=88e517f036ea9dff309e1c54d0a73d6d; sessionid=88e517f036ea9dff309e1c54d0a73d6d; sessionid_ss=88e517f036ea9dff309e1c54d0a73d6d'
}
def download(url):
"""
下载抖音无水印视频
"""
# 获取接口参数
html =requests.get(url=url, headers=headers)
print(html.status_code)
print(html.history)
print(html.url)
title = re.findall("/video/(.*?)/", html.url)[0]
print(title)
#dytk = re.findall('dytk: "(.*?)" }', html.text)[0]
# 拼接接口
url_item = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' + title
# 获取抖音无水印视频链接
html_item = requests.get(url=url_item)
print(html_item.text)
# 字符串转字典
content = json.loads(html_item.text)
print(content)
# 视频接口
url_video = content['item_list'][0]['video']['play_addr']['url_list'][0]
biaoti=content['item_list'][0]['desc']
biaoti=biaoti.replace(' ','').replace('|','')
url_video=url_video.replace('playwm','play') #无水印替换
print(url_video)
response = requests.get(url_video, headers=headers, allow_redirects=True)
redirect = response.url
print(redirect)
# 视频是二进制,需要这种下载办法
video = requests.get(url_video, headers=headers).content
video_name = biaoti+".mp4"
with open(video_name, 'wb') as f:
f.write(video)
f.flush()
print("下载完成")
if __name__ == '__main__':
# 抖音链接
url = 'https://v.douyin.com/JHWny7c/'
download(url)