import requests,re
def main(url):
douyin = url
# 请求头使用浏览器模拟的手机端请求头
headers = {
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Mobile Safari/537.36 Edg/103.0.1264.62'
}
ree = requests.get(douyin,headers=headers)
# 对页面进行重定向处理 获取新的短视频链接
new_url = ree.url
# print(new_url)
# https://www.iesdouyin.com/share/video/7120194285059788073/?region=CN&mid=6607047142617910024&u_code=if86a7jd&did=MS4wLjABAAAAxWn3bFKxVOpbMG_Ocvy7YJitc49o0gG39ucM5ohabQ0&iid=MS4wLjABAAAAlbksxOcUoFf3fEpwDkkJ3hlXXS6nK-9vB7IVcSBB1XljPwr6eDOn76tKDrf90ktL&with_sec_did=1&titleType=title×tamp=1658190604&utm_campaign=client_share&app=aweme&utm_medium=ios&tt_from=copy&utm_source=copy
# 7120194285059788073 即为视频 id
# 使用正则提取id
id = re.search(r'/video/(.*?)/',new_url).group(1)
# 提取带水印短视频链接地址
# https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=7120194285059788073
url = 'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=' + id
ree = requests.get(url, headers=headers)
wm = ree.json()
# 使用正则提取无水印视频链接
n_wm = wm['item_list'][0]['video']['play_addr']['url_list'][0].replace('wm','')
return n_wm
if __name__ == '__main__':
url = 'https://v.douyin.com/2AAVEak/'
print(main(url))
整体思路:
重定向url 提取视频id
通过视频id获取视频链接
使用正则获取无水印视频链接