操作步骤:
- 找到需要下载的视#频,点击分#享链#接
- 将里面的链接地址提取出来,复制到下方python脚本中第12行的链接,链接应该是长这样:https://v.do#uy#in.com/dGefxad/(这个url是随手写的)
- 分析过程请接着后面看
Python脚本(未处理任何异常情况,本篇主要注重过程分析,有问题的小伙伴可自行分析解决)
import json
import requests
from urllib.parse import urlparse
url = 'xxxxxxxxxxxxx' # 替换这个url
def get_shared_page(url):
if not url:
return
headers = {
'authority': 'v.douyin.com',
'method': 'GET',
'path': url.replace('https://v.douyin.com',''),
'scheme': 'https',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en,zh-CN;q=0.9,zh;q=0.8',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
res = requests.get(url,headers=headers,allow_redirects=False)
print('res is {}'.format(res.headers.get('location')))
url = res.headers.get('location')
u = urlparse(url)
item_id = u.path.split('/')[-2]
print(item_id)
video_info_url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={}'.format(item_id)
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en,zh-CN;q=0.9,zh;q=0.8',
'Connection': 'keep-alive',
'Host': 'www.iesdouyin.com',
'Referer': url,
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
res = requests.get(video_info_url,headers=headers)
json_data = json.loads(res.text)
video_id = json_data.get('item_list')[0].get('video').get('vid')
print('video_id is {}'.format(video_id))
real_url = 'https://api.amemv.com/aweme/v1/play/?video_id={}&line=1&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&is_play_url=1&source=PackSourceEnum_DOUYIN_REFLOW'.format(video_id)
headers = {
'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',
}
print('url is {}'.format(real_url))
res = requests.get(real_url,headers=headers)
with open('./video.mp4','wb') as f:
f.write(res.content)
get_shared_page(url)
分析过程后面再贴出来,主要原因是审核不过