1fy07h 发表于 2024-8-17 22:52:47

手把手教你运用Python下载抖音视频


    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/5819774f91664c5ebe35d7df2da217e5~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896527&amp;x-signature=BVTiz7FtFFA3m%2B8D83iHpZBqDT4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>平时在刷抖音的时候, 经常会看到<span style="color: black;">有些</span>优秀的内容, 想要<span style="color: black;">保留</span>下来。然而尴尬的事情<span style="color: black;">显现</span>了,要么下载的视频有水印,要么视频不<span style="color: black;">准许</span>下载,这个时候是不是只能点赞兴叹。<span style="color: black;">不碍事</span>,今天<span style="color: black;">咱们</span><span style="color: black;">运用</span> Python 来<span style="color: black;">循序渐进</span>将视频下载下来,<span style="color: black;">况且</span><span style="color: black;">无</span>水印。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">先看一下运行结果:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/2608fae2f5074b5a9798f60dc8b4cfad~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896527&amp;x-signature=sY%2Fkv9vWZz5C0mBO6%2B7x%2BR1EelE%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下载视频</p>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运行完成后,在工程文件夹<span style="color: black;">能够</span>看到,视频<span style="color: black;">已然</span>下载到本地,并且<span style="color: black;">无</span>水印</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/d69c52290a784fb0b0e007027548255e~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896527&amp;x-signature=4et21jR57l8kbtzLyyKrINAZP7M%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下载后的<span style="color: black;">照片</span></p>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">接下来, <span style="color: black;">咱们</span><span style="color: black;">仔细</span>讲解一下下载思路,其中<span style="color: black;">运用</span>了</span><strong style="color: blue;">requests</strong>模块请求网络。</p><strong style="color: blue;"><span style="color: black;">获取视频<span style="color: black;">位置</span></span></strong>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/1ccf45f78f13494d8fcc3cb185aed97a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896527&amp;x-signature=PiH297JVVHvGFOj15kck0DyD6H8%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">获取<span style="color: black;">位置</span></p>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">点击电脑端分享, <span style="color: black;">选取</span>复制链接(手机端<span style="color: black;">一样</span>的操作),<span style="color: black;">而后</span>就能获取到一下内容</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.56 h@O.xs dnd:/ 11/09 评论区抽一个人,没奖励,纯抽# 我的女团阵容 # 魔性律动舞</p>https://v.douyin.com/i8Br9jbr/ 复制此链接,打开Dou音搜索,直接观看视频!

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>正则提取分享链接中的短网址</span></p>share_url = input(<span style="color: black;">"请粘贴视频<span style="color: black;">位置</span>: "</span>)
    match = re.search(<span style="color: black;">r"https://.*/"</span>, share_url)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">抖音分享的链接中是短连接,直接<span style="color: black;">拜访</span>得到的内容并不是<span style="color: black;">实质</span>的页面</span></span><span style="color: black;">。<span style="color: black;">因此呢</span><span style="color: black;">咱们</span><span style="color: black;">必须</span>分析从短连接到<span style="color: black;">实质</span>视频页面的是<span style="color: black;">怎样</span>流转的。</span></p><strong style="color: blue;"><span style="color: black;">分析短链接<span style="color: black;">位置</span><span style="color: black;">转</span></span></strong>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">短链接是将较长的URL转换为一个较短的,易于记忆和分享的链接。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>浏览器<span style="color: black;">拜访</span>短连接<span style="color: black;">位置</span>,<span style="color: black;">发掘</span>一共经过了两次重定向, 才<span style="color: black;">最后</span>跳到了<span style="color: black;">实质</span>的视频<span style="color: black;">位置</span>。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/cc4ba92a3739426fab066ac4506709b4~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896527&amp;x-signature=Y6pv6nHK%2BAcRYnLNOZn6eNtw8AA%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">网址重定向</p>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此呢</span><span style="color: black;">必须</span>编写一个<span style="color: black;">办法</span>, 获取每次重定向的<span style="color: black;">位置</span>,多次<span style="color: black;">转</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span>递归调用,直到返回None。</span></p><span style="color: black;"># 获取重定向<span style="color: black;">位置</span> </span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__get_redirect_url</span><span style="color: black;">(url)</span>:</span>
    res = requests.get(url)
    <span style="color: black;">return</span> res.history[<span style="color: black;">0</span>].headers[<span style="color: black;">Location</span>] <span style="color: black;">if</span>len(res.history) &gt;<span style="color: black;">0</span> <span style="color: black;">else</span> <span style="color: black;">None</span><strong style="color: blue;"><span style="color: black;">获取到重定向页面后,<span style="color: black;">起始</span>分析,页面中是<span style="color: black;">怎样</span>获取视频播放<span style="color: black;">位置</span></span></strong>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">打开浏览器<span style="color: black;">掌控</span>台在网络请求<span style="color: black;">发掘</span>,视频链接是<span style="color: black;">经过</span>页面加载完成后,发起的XHR获取的, 其中请求param中</span><strong style="color: blue;"><span style="color: black;">aweme_id为视频id</span></strong></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/0152f264e84646bb801cd2187fa8fc5e~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723896527&amp;x-signature=B6j%2F3SypgLW1Zf1%2FcmUxwX0Y7W4%3D" style="width: 50%; margin-bottom: 20px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">获取视频网址</p>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">晓得</span>请求后, <span style="color: black;">咱们</span>就<span style="color: black;">能够</span>构建链接请求,其中请求<span style="color: black;">位置</span>是固定的,请求参数动态传入</span></p>res = requests.<span style="color: black;">get</span>(<span style="color: black;">https://www.douyin.com/aweme/v1/web/aweme/detail/</span>,
    <span style="color: black;">params</span>=_params, cookies=_cookies,
    headers=_headers)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">请求接口返回的是JSON数据,解析响应结果, 提取视频网址(响应结果其中<span style="color: black;">包括</span>了<span style="color: black;">区别</span>分辨率的视频,感兴趣的<span style="color: black;">能够</span>探索一下,<span style="color: black;">这儿</span><span style="color: black;">运用</span>默<span style="color: black;">看清</span>晰度)</span></p>video<span style="color: black;">_url = res.json()["aweme_</span>detail"][<span style="color: black;">"video"</span>][<span style="color: black;">"play_addr"</span>][<span style="color: black;">"url_list"</span>][<span style="color: black;">0</span>]<strong style="color: blue;"><span style="color: black;">获取视频<span style="color: black;">位置</span>后,<span style="color: black;">起始</span>构建下载视频请求, 并将其<span style="color: black;">保留</span>文件中</span></strong><span style="color: black;">with</span> requests.get(video_url, headers={
    <span style="color: black;">"Referer"</span>: video_url,
    <span style="color: black;">user-agent</span>: <span style="color: black;">Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36</span>,
    }, <span style="color: black;">timeout</span>=<span style="color: black;">60</span>) <span style="color: black;">as</span> r, <span style="color: black;">open</span>(<span style="color: black;">"./video.mp4"</span>, <span style="color: black;">mode</span>=<span style="color: black;">"wb"</span>) <span style="color: black;">as</span> <span style="color: black;">file</span>:<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">视频下载过程中可能会<span style="color: black;">必须</span>等待<span style="color: black;">有些</span>时间,<span style="color: black;">能够</span>单独起一个线程去下载。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">以上</span>获取视频<span style="color: black;">位置</span>的时候, cookie 中需要设置ttwid 和 s_v_web_id (</span><span style="color: black;"><span style="color: black;">两个值<span style="color: black;">能够</span>从浏览器 cookie 中获取</span></span><span style="color: black;">), 否则, 很可能会触发验证码<span style="color: black;">或</span>请求到空数据。网上有<span style="color: black;">非常多</span>资料解释这两个值,感兴趣的<span style="color: black;">能够</span><span style="color: black;">自动</span>搜索,本文不做<span style="color: black;">太多</span>描述。(以上方式在2023年12月16日<span style="color: black;">运用</span>有效,后续<span style="color: black;">倘若</span><span style="color: black;">运用</span><span style="color: black;">反常</span>,<span style="color: black;">能够</span>评论区踢我以下)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有<span style="color: black;">必须</span>源代码的<span style="color: black;">能够</span>点赞,评论<span style="color: black;">一起</span>学习。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">#<span style="color: black;">文案</span>首发挑战赛#</a></p>




vixf 发表于 2024-8-21 20:56:26

你的见解独到,让我受益匪浅,非常感谢。

4zhvml8 发表于 2024-10-9 13:02:03

我完全同意你的看法,期待我们能深入探讨这个问题。

m5k1umn 发表于 2024-10-29 23:40:28

网站建设seio论坛http://www.fok120.com/

4zhvml8 发表于 2024-11-6 08:35:52

你的努力一定会被看见,相信自己,加油。
页: [1]
查看完整版本: 手把手教你运用Python下载抖音视频