Python爬取快手主页视频

Python 投稿 69100 0 评论

Python爬取快手主页视频

自从快手出了网页版,使用Python爬起来爽多了,再也不用fiddler抓包了。

import json
import re
import os
import requests
import urllib.request
from multiprocessing import Pool
 
requestUrl = 'https://video.kuaishou.com/graphql'
folder_path = 'D:\kuaishou'
userId=''
cookie = ''
pcursor = ''
 
def post(userId,Cookie,pcursor):
    data = {"operationName":"visionProfilePhotoList","variables":{"userId":userId,"pcursor":pcursor,"page":"profile"},"query":"query 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      type\n      author {\n        id\n        name\n        following\n        headerUrl\n        headerUrls {\n          cdn\n          url\n          __typename\n        }\n        __typename\n      }\n      tags {\n        type\n        name\n        __typename\n      }\n      photo {\n        id\n        duration\n        caption\n        likeCount\n        realLikeCount\n        coverUrl\n        coverUrls {\n          cdn\n          url\n          __typename\n        }\n        photoUrls {\n          cdn\n          url\n          __typename\n        }\n        photoUrl\n        liked\n        timestamp\n        expTag\n        animatedCoverUrl\n        stereoType\n        videoRatio\n        __typename\n      }\n      canAddComment\n      currentPcursor\n      llsid\n      status\n      __typename\n    }\n    hostName\n    pcursor\n    __typename\n  }\n}\n"}
    failed = {'msg': 'failed...'}
    headers = {
        'Host':'video.kuaishou.com',
        'Connection':'keep-alive',
        'Content-Length':'1261',
        'accept':'*/*',
        'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/89.0.4389.114Safari/537.36Edg/89.0.774.68',
        'content-type':'application/json',
        'Origin':'https://video.kuaishou.com',
        'Sec-Fetch-Site':'same-origin',
        'Sec-Fetch-Mode':'cors',
        'Sec-Fetch-Dest':'empty',
        'Referer':'https://video.kuaishou.com/profile/' + userId,
        'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cookie':Cookie,
 
    }
    r = requests.post(requestUrl, data=json.dumps(data), headers=headers)  
    r.encoding = 'UTF-8'
    html = r.text
    return html
def down(feeds):
    for feed in feeds:
        #print(feed['photo']['caption'])
        #print(feed['photo']['photoUrl'])
        author = feed['author']['name']
        filename = feed['photo']['caption']
        if not os.path.exists(folder_path + '/' + author + '/'):
            os.makedirs(path)
        filepath = folder_path + '/' + author + '/' + filename + '.mp4'
        filepath.replace("~", "")
        if not os.path.exists(filepath):
            urllib.request.urlretrieve(feed['photo']['photoUrl'], filename=filepath)
            print(filepath + ",下载完成")
        else:
            print(filepath + ",已存在,跳过")
 
if __name__ == "__main__":
     
     
    p = Pool(10)
    while True:
        result = post(userId,cookie,pcursor)
        data = json.loads(result)
        pcursor = data['data']['visionProfilePhotoList']['pcursor']
        feeds = data['data']['visionProfilePhotoList']['feeds']
        for feed in feeds:
            print(feed['photo']['caption'])
        p.apply_async(down, args=(feeds,))
        #down(feeds)
        if pcursor is '':
            break
 
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

编程笔记 » Python爬取快手主页视频

赞同 (174) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽