Python 案例 之 视频下载 以及实现自动评论/点赞

前言

大家早好、午好、晚好吖 ❤ ~欢迎光临本文章

知识点:

  • 动态数据抓包

  • requests发送请求

开发环境:

  • python 3.8 运行代码

  • pycharm 2022.3 辅助敲代码

  • requests pip install requests

代码展示

下载视频

import requests     # 发送请求的第三方库 需要额外安装
import os

if not os.path.exists("video"):
    os.mkdir("video")
# 如果是post请求
#   请求头
headers = {
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Length': '1749',
    'Cookie': 'kpf=PC_WEB; clientid=3; did=web_4bf90ac4df8aaef2d99527f1da1063fd; didv=1678167635560; _bl_uid=pXlbpeg8xa9tn1o9hjmj3R0ytXyC; clientid=3; kpf=PC_WEB; kpn=KUAISHOU_VISION; _did=web_395449265AAE9704; userId=3293066791; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABa8n2Oser9PR4X2hmbbbLTcOtL58WemkEmKHC9W4MGqmCyL6R0egMYhuuJxnMxqrnRxgyJJ6my4E6FXkG3osKKoOpkiLCHivXNJnmB4nUOlfyTTgQFgjNrrg4wKjV5Bj9Km7aN1XTrvCXVrnDoGBaWxMCbCQEssTwHEpql-M86HXNwOfUvyMbClKGRdvFEJXHq4_ykR8zB59EMO4jPXqGRhoStVKEb-xUGkLo9u0A7O3lj4AGIiC2Pi6EGHIV2sTd0lAbXwq_QIgv6k_rk7z6YyhzeRC55igFMAE; kuaishou.server.web_ph=bfee5d3107a6e32d23b76a44663333ef9213; kpn=KUAISHOU_VISION',
    'Host': '****',
    'Origin': '*****',
    'Pragma': 'no-cache',
    'Referer': '*****/short-video/3xh29dtrcdy5trg?authorId=3xgrvv9za78s7te&streamSource=profile&area=profilexxnull',
    '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/114.0.0.0 Safari/537.36',
    'accept': '*/*',
    'content-type': 'application/json',
    'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}
#   请求体
源码、解答、教程等加V:qian97378免费领取
json = {"operationName":"visionProfilePhotoList","variables":{"userId":"3xgrvv9za78s7te","pcursor":"","page":"detail","webPageArea":"profilexxnull"},"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"}
url = 'https://www.kuaishou.com/graphql'
response = requests.post(url=url, headers=headers, json=json)
# 将 所有的 视频链接取出来
json_data = response.json()
feeds = json_data['data']['visionProfilePhotoList']['feeds']
for feed in feeds:
    photoUrl = feed['photo']['photoUrl']
    caption = feed['photo']['caption']
    video_data = requests.get(photoUrl).content
    open("video/"+caption+".mp4", mode='wb').write(video_data)

自动评论

# import requests
#
# cookies = {
#     'kpf': 'PC_WEB',
#     'clientid': '3',
#     'did': 'web_4bf90ac4df8aaef2d99527f1da1063fd',
#     'didv': '1678167635560',
#     '_bl_uid': 'pXlbpeg8xa9tn1o9hjmj3R0ytXyC',
#     'clientid': '3',
#     'kpf': 'PC_WEB',
#     'kpn': 'KUAISHOU_VISION',
#     '_did': 'web_395449265AAE9704',
#     'userId': '3293066791',
#     'kuaishou.server.web_st': 'ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABy3ou4O15k0sc9oVavtz6PHanUfFVLe9pkIX45WCZSGNQbq3yG5P10ZWbQOa35d_TUfbZZvO5C5NRbQqoVNKHyHw8NPr5QTIOzX-YtjBOFWsFwbcNi1sLBwOWIMe_B8msW5h1tCDLpwrKRseoidmRf09oSHgZt4xtblDvAKnBgDA_bcRN0_sMiYdVpUrlFYj1vXu6XuMwtLi8e3iVWqbGFhoScSev-lUTUQRM3QfVIiGGGpa1IiDH-K2LGfeK_fY01eIqzaA8Ybr_nLKZDv-zUYKXiNT2bigFMAE',
#     'kuaishou.server.web_ph': 'e56949d1cbe08cd48838f86d7143e2d47652',
#     'kpn': 'KUAISHOU_VISION',
#     'ktrace-context': '1|MS43NjQ1ODM2OTgyODY2OTgyLjkyMjE5MTkxLjE2ODk3NzAwNzM4OTEuMTM1MjMw|MS43NjQ1ODM2OTgyODY2OTgyLjI1NjI1NDM2LjE2ODk3NzAwNzM4OTEuMTM1MjMx|0|graphql-server|webservice|false|NA',
# }
#
# headers = {
#     'Accept-Language': 'zh-CN,zh;q=0.9',
#     'Cache-Control': 'no-cache',
#     'Connection': 'keep-alive',
#     源码、解答、教程等加V:qian97378免费领取
#     # Requests sorts cookies= alphabetically
#     # 'Cookie': 'kpf=PC_WEB; clientid=3; did=web_4bf90ac4df8aaef2d99527f1da1063fd; didv=1678167635560; _bl_uid=pXlbpeg8xa9tn1o9hjmj3R0ytXyC; clientid=3; kpf=PC_WEB; kpn=KUAISHOU_VISION; _did=web_395449265AAE9704; userId=3293066791; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABy3ou4O15k0sc9oVavtz6PHanUfFVLe9pkIX45WCZSGNQbq3yG5P10ZWbQOa35d_TUfbZZvO5C5NRbQqoVNKHyHw8NPr5QTIOzX-YtjBOFWsFwbcNi1sLBwOWIMe_B8msW5h1tCDLpwrKRseoidmRf09oSHgZt4xtblDvAKnBgDA_bcRN0_sMiYdVpUrlFYj1vXu6XuMwtLi8e3iVWqbGFhoScSev-lUTUQRM3QfVIiGGGpa1IiDH-K2LGfeK_fY01eIqzaA8Ybr_nLKZDv-zUYKXiNT2bigFMAE; kuaishou.server.web_ph=e56949d1cbe08cd48838f86d7143e2d47652; kpn=KUAISHOU_VISION; ktrace-context=1|MS43NjQ1ODM2OTgyODY2OTgyLjkyMjE5MTkxLjE2ODk3NzAwNzM4OTEuMTM1MjMw|MS43NjQ1ODM2OTgyODY2OTgyLjI1NjI1NDM2LjE2ODk3NzAwNzM4OTEuMTM1MjMx|0|graphql-server|webservice|false|NA',
#     'Origin': '****',
#     'Pragma': 'no-cache',
#     'Referer': '*****/short-video/3x2wc6chy5u2uky?authorId=3xgrvv9za78s7te&streamSource=profile&area=profilexxnull',
#     '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/114.0.0.0 Safari/537.36',
#     'accept': '*/*',
#     # Already added when you pass json=
#     # 'content-type': 'application/json',
#     'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
#     'sec-ch-ua-mobile': '?0',
#     'sec-ch-ua-platform': '"Windows"',
# }
#
# json_data = {
#     'operationName': 'visionAddComment',
#     'variables': {
#         'photoId': '3x2wc6chy5u2uky',
#         'photoAuthorId': '3xgrvv9za78s7te',
#         'content': '77777',
#         'expTag': '1_a/2006220372254483169_xpcwebprofilexxnull0',
#     },
#     'query': 'mutation visionAddComment($photoId: String, $photoAuthorId: String, $content: String, $replyToCommentId: ID, $replyTo: ID, $expTag: String) {\n  visionAddComment(photoId: $photoId, photoAuthorId: $photoAuthorId, content: $content, replyToCommentId: $replyToCommentId, replyTo: $replyTo, expTag: $expTag) {\n    result\n    commentId\n    content\n    timestamp\n    status\n    __typename\n  }\n}\n',
# }
#
# response = requests.post('****/graphql', cookies=cookies, headers=headers, json=json_data)

自动点赞

import requests

cookies = {
    'kpf': 'PC_WEB',
    'clientid': '3',
    'did': 'web_4bf90ac4df8aaef2d99527f1da1063fd',
    'didv': '1678167635560',
    '_bl_uid': 'pXlbpeg8xa9tn1o9hjmj3R0ytXyC',
    'clientid': '3',
    'kpf': 'PC_WEB',
    'kpn': 'KUAISHOU_VISION',
    '_did': 'web_395449265AAE9704',
    'userId': '3293066791',
    'ktrace-context': '1|MS43NjQ1ODM2OTgyODY2OTgyLjkyMjE5MTkxLjE2ODk3NzAwNzM4OTEuMTM1MjMw|MS43NjQ1ODM2OTgyODY2OTgyLjI1NjI1NDM2LjE2ODk3NzAwNzM4OTEuMTM1MjMx|0|graphql-server|webservice|false|NA',
    'kuaishou.server.web_st': 'ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABmUP1mgNo5RobiujBC7GAzA1Kg3hOUbrkoiNZSgsUgk41FyJJjUqWnwweLTdWXvXRPzTQdunogCLVUCna2cASd4eEo7d-C-vgP9rFfRyOhP1yhCT1rz3pvU0LZmo63PeDaQjl6nRqNUyYmlO1e-1O_phROzVO5qpMe3QeeK_8jyQrnTQaNh87VhTZESiM7WYVfJTsghbTo2OdWM-wk5DmyxoSaJdVn4X6xsr-Lm_zFsYrKwMBIiDk1SjBJ7u5Tqc3f283N8age9ffUbnlMiaqJQ3qIzfnaigFMAE',
    'kuaishou.server.web_ph': '4a35184da74feb13a5d6907ff3b3c023bd19',
    'kpn': 'KUAISHOU_VISION',
}

headers = {
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Origin': '*****',
    'Pragma': 'no-cache',
    'Referer': '*****/short-video/3x2wc6chy5u2uky?authorId=3xgrvv9za78s7te&streamSource=profile&area=profilexxnull',
    '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/114.0.0.0 Safari/537.36',
    'accept': '*/*',
    'sec-ch-ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

json_data = {
    'operationName': 'visionVideoLike',
    'variables': {
        'photoId': '3x2wc6chy5u2uky',
        'photoAuthorId': '3xgrvv9za78s7te',
        'cancel': 0,
        'expTag': '1_a/2006177002144867233_xpcwebprofilexxnull0',
    },
    'query': 'mutation visionVideoLike($photoId: String, $photoAuthorId: String, $cancel: Int, $expTag: String) {\n  visionVideoLike(photoId: $photoId, photoAuthorId: $photoAuthorId, cancel: $cancel, expTag: $expTag) {\n    result\n    __typename\n  }\n}\n',
}

response = requests.post('******/graphql', cookies=cookies, headers=headers, json=json_data)

尾语

好了,今天的分享就差不多到这里了!

完整代码、更多资源、疑惑解答直接点击下方名片自取即可。

对下一篇大家想看什么,可在评论区留言哦!看到我会更新哒(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

最后,宣传一下呀~更多源码、资料、素材、解答、交流皆点击下方名片获取呀

你可能感兴趣的:(爬虫小案例,python,音视频,开发语言,pycharm,学习)