欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

[爬虫]随机下载短视频工具,只需输入名称和地址

最编程 2024-10-09 07:17:40
...
import requests
import re
import os
episodeNumber = 0
title = input("请你输入你的下载的短视频名称:")
if not os.path.exists(title):
    os.mkdir(title)
url =  input("请你输入你的短视频网址:")
tubeId = re.findall("tubeId=(.*?)&",url)[0]
while True:
    cookies = {
        'kpf': 'PC_WEB',
        'clientid': '3',
        'did': 'web_54c72039dc16f496133eef1f656266c1',
        'kpn': 'KUAISHOU_VISION',
    }

    headers = {
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        # 'Cookie': 'kpf=PC_WEB; clientid=3; did=web_54c72039dc16f496133eef1f656266c1; kpn=KUAISHOU_VISION',
        'Origin': 'https://www.kuaishou.com',
        'Pragma': 'no-cache',
        'Referer': 'https://www.kuaishou.com/short-video/3x7aqq3isbm7x6a?streamSource=theater&currentPcursor=1&tubeId=5xg8rrxzucwc4nk&fromPage=theater&fromChannal=0',
        '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/127.0.0.0 Safari/537.36',
        'accept': '*/*',
        'content-type': 'application/json',
        'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }

    json_data = {
        'operationName': 'visionTubeEpisodeQuery',
        'variables': {
            'tubeId': tubeId,
            'episodeNumber': episodeNumber,
            'page': 'theater',
            'channelId': 0,
        },
        'query': 'fragment photoContent on PhotoEntity {\n  __typename\n  id\n  duration\n  caption\n  originCaption\n  likeCount\n  viewCount\n  commentCount\n  realLikeCount\n  coverUrl\n  photoUrl\n  photoH265Url\n  manifest\n  manifestH265\n  videoResource\n  coverUrls {\n    url\n    __typename\n  }\n  timestamp\n  expTag\n  animatedCoverUrl\n  distance\n  videoRatio\n  liked\n  stereoType\n  profileUserTopPhoto\n  musicBlocked\n  riskTagContent\n  riskTagUrl\n}\n\nfragment recoPhotoFragment on recoPhotoEntity {\n  __typename\n  id\n  duration\n  caption\n  originCaption\n  likeCount\n  viewCount\n  commentCount\n  realLikeCount\n  coverUrl\n  photoUrl\n  photoH265Url\n  manifest\n  manifestH265\n  videoResource\n  coverUrls {\n    url\n    __typename\n  }\n  timestamp\n  expTag\n  animatedCoverUrl\n  distance\n  videoRatio\n  liked\n  stereoType\n  profileUserTopPhoto\n  musicBlocked\n  riskTagContent\n  riskTagUrl\n}\n\nfragment feedContent on Feed {\n  type\n  author {\n    id\n    name\n    headerUrl\n    following\n    headerUrls {\n      url\n      __typename\n    }\n    __typename\n  }\n  photo {\n    ...photoContent\n    ...recoPhotoFragment\n    __typename\n  }\n  canAddComment\n  llsid\n  status\n  currentPcursor\n  tags {\n    type\n    name\n    __typename\n  }\n  __typename\n}\n\nquery visionTubeEpisodeQuery($tubeId: String, $episodeNumber: Int, $page: String, $channelId: Int, $webPageArea: String) {\n  visionTubeEpisode(tubeId: $tubeId, episodeNumber: $episodeNumber, page: $page, channelId: $channelId, webPageArea: $webPageArea) {\n    ...feedContent\n    result\n    status\n    __typename\n  }\n}\n',
    }

    response = requests.post('https://www.kuaishou.com/graphql', cookies=cookies, headers=headers, json=json_data)

    # https://www.kuaishou.com/short-video/3xxhitk2szntnhe?streamSource=theater&tubeId=5xg8rrxzucwc4nk&fromPage=theater&fromChannal=0

    down_url = re.findall('"photoUrl":"(.*?)","',response.text)

    res = requests.get(down_url[0],headers=headers)
    open(f"{title}/第{episodeNumber+1}集.mp4","wb").write(res.content)
    episodeNumber+=1