实现效果
寻找接口
首先需要用手机分享一个视频的链接出来
此处例子:https://v.douyin.com/JLJyYYr/
然后新建一个无痕窗口,打开开发者工具,设置UA为移动端才可以播放
找到开发者工具的network选项卡,可以发现访问链接以后进行了302跳转,分析一下既可以发现跳转的链接中附带了视频ID,所以我们要获取到这个ID,方法有很多,可以禁止重定向跳转(在请求时加上一个参数:allow_redirects=False),或者不加限制,在请求完成后,返回请求完成的URL,然后拆分一下接口一获取到ID
接下来继续分析一下其他的请求,着重看一下与刚找到ID有关的。然后可以找到以下的链接;
然后查看一下返回的数据:
{"status_code":0,"item_list":[{"video_labels":null,"is_preview":0,"is_live_replay":false,"desc":"《哦乖!》 温室培育不出美丽的花朵,有时候要学会独立思考#百亿剧好看计划","author":{"signature":"异界万花筒,缤纷的世界。\n把你想讲的想看的故事告诉我\n说不定就是下一集呢!","unique_id":"YJWHT","platform_sync_info":null,"policy_version":null,"nickname":"异界万花筒","short_id":"2840174275","avatar_larger":{"uri":"317e30007aac3ee60a4c7","url_list":["https://p26-dy.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p9-dy.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p3-dy.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038"]},"avatar_thumb":{"url_list":["https://p9-dy.byteimg.com/aweme/100x100/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p6-dy-ipv6.byteimg.com/aweme/100x100/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p3-dy.byteimg.com/aweme/100x100/317e30007aac3ee60a4c7.jpeg?from=4010531038"],"uri":"317e30007aac3ee60a4c7"},"avatar_medium":{"uri":"317e30007aac3ee60a4c7","url_list":["https://p3-dy.byteimg.com/aweme/720x720/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p9-dy.byteimg.com/aweme/720x720/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p6-dy-ipv6.byteimg.com/aweme/720x720/317e30007aac3ee60a4c7.jpeg?from=4010531038"]},"followers_detail":null,"geofencing":null,"type_label":null,"uid":"52009932080"},"duration":149568,"author_user_id":52009932080,"category":110,"text_extra":[{"start":28,"end":36,"type":1,"hashtag_name":"百亿剧好看计划","hashtag_id":1658389496684558}],"share_info":{"share_weibo_desc":"#在抖音,记录美好生活#《哦乖!》 温室培育不出美丽的花朵,有时候要学会独立思考#百亿剧好看计划","share_desc":"在抖音,记录美好生活","share_title":"《哦乖!》 温室培育不出美丽的花朵,有时候要学会独立思考#百亿剧好看计划"},"aweme_type":4,"uniqid_position":null,"geofencing":null,"label_top_text":null,"create_time":1593401515,"video":{"width":720,"dynamic_cover":{"uri":"tos-cn-p-0015/62282b99d00347669a302a43cc187d8c_1593401519","url_list":["https://p9-dy-ipv6.byteimg.com/obj/tos-cn-p-0015/62282b99d00347669a302a43cc187d8c_1593401519?from=2563711402_large","https://p29-dy.byteimg.com/obj/tos-cn-p-0015/62282b99d00347669a302a43cc187d8c_1593401519?from=2563711402_large","https://p3-dy.byteimg.com/obj/tos-cn-p-0015/62282b99d00347669a302a43cc187d8c_1593401519?from=2563711402_large"]},"origin_cover":{"uri":"large/tos-cn-p-0015/ad985e00aaa048e8a985ed7c4f8f52e4_1593401517","url_list":["http://p9-dy.byteimg.com/large/tos-cn-p-0015/ad985e00aaa048e8a985ed7c4f8f52e4_1593401517.jpeg?from=2563711402_large","http://p1-dy.byteimg.com/large/tos-cn-p-0015/ad985e00aaa048e8a985ed7c4f8f52e4_1593401517.jpeg?from=2563711402_large","http://p3-dy.byteimg.com/large/tos-cn-p-0015/ad985e00aaa048e8a985ed7c4f8f52e4_1593401517.jpeg?from=2563711402_large"]},"ratio":"540p","has_watermark":true,"cover":{"uri":"tos-cn-p-0015/3146feed01ea4822992e9e0eb2657a51","url_list":["https://p6-dy-ipv6.byteimg.com/img/tos-cn-p-0015/3146feed01ea4822992e9e0eb2657a51~c5_300x400.jpeg?from=2563711402_large","https://p1-dy.byteimg.com/img/tos-cn-p-0015/3146feed01ea4822992e9e0eb2657a51~c5_300x400.jpeg?from=2563711402_large","https://p3-dy.byteimg.com/img/tos-cn-p-0015/3146feed01ea4822992e9e0eb2657a51~c5_300x400.jpeg?from=2563711402_large"]},"height":1280,"bit_rate":null,"duration":149568,"vid":"v0200f4f0000brslvstahtm68pc8i9mg","is_long_video":1,"play_addr":{"url_list":["https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f4f0000brslvstahtm68pc8i9mg&ratio=720p&line=0"],"uri":"v0200f4f0000brslvstahtm68pc8i9mg"}},"position":null,"video_text":null,"forward_id":"0","aweme_id":"6843607307150986504","cha_list":[{"desc":"【5月榜单出炉】https://bit.ly/2XdGXWB\n【超短片上头条招募】https://t.zijieimg.com/EeYAs3/\n你是天生的段子手,却缺少围观的群众?\n你的原创短剧风格自成一派,却没有很好的曝光?\n带话题#百亿剧好看计划,发布你的原创作品,\n即有机会获得曝光,更有超多活动奖励等你!\n\n【题材】段子、短剧\n【内容类型】搞笑、恋爱、古风、魔幻、悬幻、其他类型\n【活动奖励】官方认证、平台资源曝光、dou+ 奖励 、星图推荐、招商推荐、联合 IP 出品\n\n【活动规则详情】\n1. 投稿时间:截止 2020年5月20日 24:00;\n2.视频投稿需要添加话题#百亿剧好看计划;\n3.保证视频视频原创性,请勿盗用他人视频;\n4.请勿投递包含商业化广告意图的视频;\n5.涉嫌数据做假、违反抖音审核规则的稿件,一经举报核实将取消参与资格。\n\n【评奖规则】\n1.活动期间上传 1条视频到话题#百亿剧好看计划,即可获得榜单评奖资格;\n2.主办方将根据作者在活动期间的投稿量、视频播放量、视频点赞量、以及内综合计算作者的排名,根据排名进行奖励。\n3.2020年2月21日起每 月发布 1次榜单排名,即 3月21日、4月21日、5月21日;\n4.奖励设置:\n综合排名 1-5,每人价值 10000元dou+奖励;\n综合排名 6-10,每人价值 5000元dou+奖励。\n\n【奖品发放注意事项】\n1.榜单发布 7个工作日内,抖音官方工作人员将通过系统消息与获奖者沟通领奖事宜;\n2.本活动不向获奖用户收取任何费用,注意防范网络诈骗;\n3.本活动与【Apple Inc. 无关】。","user_count":0,"type":2,"view_count":0,"hash_tag_profile":"2f71100067f62ffa53643","is_commerce":true,"cha_name":"百亿剧好看计划","connect_music":null,"cover_item":{"uri":"2f71100067f62ffa53643","url_list":["https://p3-dy.bytecdn.cn/live/100x100/2f71100067f62ffa53643.jpg","https://p9-dy.bytecdn.cn/live/100x100/2f71100067f62ffa53643.jpg","https://p9-dy.bytecdn.cn/live/100x100/2f71100067f62ffa53643.jpg"]},"cid":"1658389496684558"}],"image_infos":null,"share_url":"https://www.iesdouyin.com/share/video/6843607307150986504/?region=&mid=6843607451657358094&u_code=48&titleType=title","statistics":{"aweme_id":"6843607307150986504","comment_count":151,"digg_count":1548},"comment_list":null,"risk_infos":{"warn":false,"type":0,"content":""},"long_video":null,"group_id":6843349387003956000,"music":{"title":"@异界万花筒创作的原声","cover_large":{"uri":"317e30007aac3ee60a4c7","url_list":["https://p3-dy-ipv6.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p6-dy-ipv6.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p9-dy.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038"]},"position":null,"cover_thumb":{"uri":"317e30007aac3ee60a4c7","url_list":["https://p9-dy-ipv6.byteimg.com/aweme/100x100/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p6-dy-ipv6.byteimg.com/aweme/100x100/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p26-dy.byteimg.com/aweme/100x100/317e30007aac3ee60a4c7.jpeg?from=4010531038"]},"play_url":{"uri":"http://p1-dy.byteimg.com/obj/ies-music/6843607418841090829.mp3","url_list":["http://p1-dy.byteimg.com/obj/ies-music/6843607418841090829.mp3","http://p3-dy.byteimg.com/obj/ies-music/6843607418841090829.mp3"]},"duration":149,"id":6843607451657358000,"mid":"6843607451657358094","author":"异界万花筒","cover_hd":{"uri":"317e30007aac3ee60a4c7","url_list":["https://p3-dy-ipv6.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p6-dy-ipv6.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p9-dy.byteimg.com/aweme/1080x1080/317e30007aac3ee60a4c7.jpeg?from=4010531038"]},"cover_medium":{"uri":"317e30007aac3ee60a4c7","url_list":["https://p26-dy.byteimg.com/aweme/720x720/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p9-dy.byteimg.com/aweme/720x720/317e30007aac3ee60a4c7.jpeg?from=4010531038","https://p3-dy.byteimg.com/aweme/720x720/317e30007aac3ee60a4c7.jpeg?from=4010531038"]},"status":1},"promotions":null}],"extra":{"now":1593402772000,"logid":"2020062911525201001404003309051BC8"}}
格式化输出一下,发现了一个这个键值对:
这个就是获取视频的接口了,然后去除水印的话,只需要把接口地址中的playwm替换成play(去掉wm就行)
实现
代码:
import requests
import json
#获取抖音地址
url = input("请输入抖音地址:")
header = {
"Referer": "https://v.douyin.com/",
'User-Agent': "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36",
}
#获取到视频id
r = requests.get(url, headers = header)
url = r.url
id = url.split("video/")[1].split("/?")[0]
#获取到有水印视频地址
url="https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + id
r = requests.get(url, headers = header)
#获取到无水印视频地址
video = json.loads(r.content)["item_list"][0]
url = video["video"]["play_addr"]["url_list"][0]
url = url.replace("playwm","play")
#开始下载
r = requests.get(url,headers = header)
with open(video["desc"]+".mp4", "wb") as code:
code.write(r.content)
print("下载成功")