快手解析视频真实链接(爬取快手视频)

December 09, 2023
测试
测试
测试
测试
8 分钟阅读

[快手解析视频真实链接(爬取快手视频)]

1.复制快手视频链接(如下)

陪伴是最常情的告白,守护是最沉默的陪伴…… #汪星人 #宠物避障挑战 https://v.kuaishou.com/5xXNiL 复制此链接,打开【快手App】直接观看!

虚拟链接是:https://v.kuaishou.com/5xXNiL

2.代码解析真实链接(这里就不介绍代码,有问题可以留言)

# -*- coding: utf-8 -*-
"""
Created on Fri May 15 13:17:21 2020
@author: 李运辰
"""

import requests
requests.packages.urllib3.disable_warnings()
headers = {
   
    #
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
}
headers2 = {
    #"Host":"music.liuzhijin.cn",
    "Host": "live.kuaishou.com",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
    "Cookie": "did=web_c0f3196ec94d4837b5f7850e3ebac3b9; didv=1589520098000; clientid=3; client_key=65890b29", 
}
def geturl(url0):
    #url0="https://v.kuaishou.com/5loz4u"
    res0 = requests.get(url0,headers=headers,verify=False)
    """转接第二段"""
    
    cookie = res0.cookies.get_dict()
    cookie = str(cookie).replace("{","").replace("}","").replace(" ","").replace("'","").replace(",",";")
    
    headers3 ={
            "Host": "v.kuaishou.com",
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
            "Cookie":cookie.replace(":","=")
            }
    
    headers4 ={
            "Host": "live.kuaishou.com",
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
            "Cookie":cookie.replace(":","=")
            }
    res1 = requests.get(url0,headers=headers3,allow_redirects=False)
    url2 = res1.headers['Location']
    
    url_00 = url2.split("userId=")[1].split("&")[0]
    
    
    """第一部分url"""
    url_0=url2.split("?")[0].split("/")[-1]
    res2 = requests.get(url2,headers=headers3,allow_redirects=False).request.headers
    
    """第二部分url"""
    url_1 = res2['Cookie'].split(";")[-1].replace(":","=")
    
    """完整url"""
    url = "https://live.kuaishou.com/u/"+url_00+"/"+url_0+"?"+url_1
    #print(url)
    
    response  = requests.get(url,headers=headers4)
    text = response.text
    
    """视频链接"""
    v_url =text.split('"playUrl":"')[1].split(".mp4")[0]+".mp4"
    v_url = v_url.replace("u002F","")
    #print(v_url)
    return v_url


st="陪伴是最常情的告白,守护是最沉默的陪伴…… #汪星人 #宠物避障挑战 https://v.kuaishou.com/5xXNiL 复制此链接,打开【快手App】直接观看!"
st ="http"+(st.split("复制")[0].split("http")[1].replace(" ",""))
u = geturl(st)
print(u)

3.解析结果如下:

感谢优秀的你关注我

~ 正在学习爬虫的你,可以在“学习资料”专栏获取学习资料 如果需要哪些python有关的学习资料,欢迎留言~

继续阅读

更多来自我们博客的帖子

如何安装 BuddyPress
由 测试 December 17, 2023
经过差不多一年的开发,BuddyPress 这个基于 WordPress Mu 的 SNS 插件正式版终于发布了。BuddyPress...
阅读更多
Filter如何工作
由 测试 December 17, 2023
在 web.xml...
阅读更多
如何理解CGAffineTransform
由 测试 December 17, 2023
CGAffineTransform A structure for holding an affine transformation matrix. ...
阅读更多