Python 下载快手视频

电脑登录快手,获取id填入代码

1:根据分类下载

import requests
import re
import random
import string

url = "https://www.kuaishou.com/graphql"
headers = {
    "Content-Type": "application/json",
    # (其他请求头...)
     "Cookie":"kpf=PC_WEB; clientid=3; did=web_8c4d1823177313d16ecfab125dd7ba0e; userId=3684484720; kuaishou.server.webday7_st=ChprdWFpc2hvdS5zZXJ2ZXIud2ViZGF5Ny5zdBKwAWECHeKvgSCTgfgv5Y8GmW_-L02QzdCYfOcteQI0_58usjSU8fFXcSb_eUEsMaeSoUWSgmc_M5wr_1vmIpLwqdm9hQkfiV1sVNY-8Cza0aU9Q2KxIvH4GLFGtxlw8qACceXlSDpxPX0VpnDjErzrrMiYOD5o_IiCy-3ksr-zk0XnlncwpffkgUR6qkEA13mj3PGSPUQrfBmkCc32eSjMC5HQElkiVFIgcwmrcBA8yg6UGhLnL33oyPAVhFFV1o7h2Db3JhgiILTupl5Bj5T5OHsAss9c3hIr6nWmgsGmdGjpU6zFaq1rKAUwAQ; kuaishou.server.webday7_ph=ec8f9050cbcd6669bbd978e64bb0e5095225; kpn=KUAISHOU_VISION",
    "Host": "www.kuaishou.com",
    "Origin": "https://www.kuaishou.com",
    "Referer": "https://www.kuaishou.com/profile/3xdyx67349gv4pq",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",

}


def generate_random_string(length):
    letters = string.ascii_lowercase + string.ascii_uppercase + string.digits
    return ''.join(random.choice(letters) for _ in range(length))


def get_page(pcursor, video_index=1):
    data = {
        # (GraphQL请求数据...)
         "operationName": "visionSearchPhoto",
        "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}\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}\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 visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {\n  visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {\n    result\n    llsid\n    webPageArea\n    feeds {\n      ...feedContent\n      __typename\n    }\n    searchSessionId\n    pcursor\n    aladdinBanner {\n      imgUrl\n      link\n      __typename\n    }\n    __typename\n  }\n}\n",
        "variables": {"keyword": '库', "pcursor": pcursor, "page": "search"},
    }

    if pcursor is None:
        print('全部下载完成')
        return

    res = requests.post(url=url, headers=headers, json=data)
    json_data = res.json()
    feeds = json_data["data"]["visionSearchPhoto"]["feeds"]
    pcursor = json_data['data']['visionSearchPhoto']['pcursor']

    for index, feed in enumerate(feeds, start=video_index):
        caption = feed['photo']['caption']
        # 打印的视频名称
        # print(caption)
        if not caption:
            caption = generate_random_string(10)
        caption = re.sub('[\\/:*?<>|\n\t]', '', caption)
        photoUrl = feed['photo']['photoUrl']
        video_data = requests.get(url=photoUrl).content

        # caption 是视频的名称 index 是视频的序号
        # with open(f'test/{index}_{caption}.mp4', mode='wb') as f:
        with open(f'y/{index}.mp4', mode='wb') as f:
            f.write(video_data)
        print(f"视频:{index}_{caption} 下载完成!")

    get_page(pcursor, video_index + len(feeds))


get_page('')

 

2:根据主页

import requests
import re
import random
import string

url = "https://www.kuaishou.com/graphql"

id = '3xeexnjqn2iwk8q'  # 爬取用户的ID  电脑登陆之后,用户主页地址栏后面那一串

headers = {
    "Content-Type": "application/json",
    "Cookie": "kpf=PC_WEB; clientid=3; did=web_a8d5db30dc5321c11e2e39a3573feb80; userId=3684483109; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqAB9KxtiTWdV0495aWAXx5pLbDJFcXxzw495pDBjEPXBO0KZ3H4oLjUdSDDh0-Hb1ok_ZbikU2nGU56vlLKXagIAAtmvxBfNuiiq97TJcGmW0msUVXKHwY7uLAQZ-MRJiwdJkygr8olzOopmkPYvYRCBFF3LHUIx5afAfMBl1r7-AXtaAMaMCZsZckF2kCIkrL_gZQiLoXybOZFK3-7TQzeTxoS5dGNQ2tN9j6L3QVO7fJXKiWdIiDhvqKvMwRZShLdhafJ5HuE4BLa8aPtvJ3JMuoYPq6AsCgFMAE; kuaishou.server.web_ph=fe3cf52fe33e5b359da48fd30a84901d7070; kpn=KUAISHOU_VISION",
    "Host": "www.kuaishou.com",
    "Origin": "https://www.kuaishou.com",
    "Referer": "https://www.kuaishou.com/profile/3xdyx67349gv4pq",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
}


def generate_random_string(length):
    letters = string.ascii_lowercase + string.ascii_uppercase + string.digits
    return ''.join(random.choice(letters) for _ in range(length))


def get_page(pcursor):
    data = {
        "operationName": "visionProfilePhotoList",
        "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}\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}\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 visionProfilePhotoList($pcursor: String, $userId: String, $page: String, $webPageArea: String) {\n  visionProfilePhotoList(pcursor: $pcursor, userId: $userId, page: $page, webPageArea: $webPageArea) {\n    result\n    llsid\n    webPageArea\n    feeds {\n      ...feedContent\n      __typename\n    }\n    hostName\n    pcursor\n    __typename\n  }\n}\n",
        "variables": {"userId": id, "pcursor": pcursor, "page": "profile"},
    }

    if pcursor is None:
        print('全部下载完成')
        return

    res = requests.post(url=url, headers=headers, json=data)
    json_data = res.json()
    feeds = json_data["data"]["visionProfilePhotoList"]["feeds"]
    pcursor = json_data['data']['visionProfilePhotoList']['pcursor']

    for index, feed in enumerate(feeds, start=1):
        caption = feed['photo']['caption']
        if not caption:
            caption = generate_random_string(10)
        caption = re.sub('[\\/:*?<>|\n\t]', '', caption)
        photoUrl = feed['photo']['photoUrl']
        video_data = requests.get(url=photoUrl).content
        with open(f'ks/{caption}_{index}.mp4', mode='wb') as f:
            f.write(video_data)
        print(f"视频 {caption}_{index} 下载完成!")

    get_page(pcursor)


get_page('')

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容