序言
不那么好走的路,才是上坡路.
所有往上的路,都与我们本能的懒惰、懈怠和拖延不相容。
当你觉得很苦很累时,不要泄气,因为你可能走在上坡的路上。
实战
第一步:请求网络;获取到网站返回的数据内容
import requests
cookies = {
'did': 'web_87ee5530f7dc26c5c05dfe66acf70e14',
'didv': '1649420428219',
'kpf': 'PC_WEB',
'kpn': 'KUAISHOU_VISION',
'clientid': '3',
'client_key': '65890b29',
'ksliveShowClipTip': 'true',
'userId': '16671983',
'kuaishou.server.web_st': 'ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABSRZDXsMuMvNx1jD8ZrUbZx_qeCKWWqkVg-4tSrPfgTQp7QUnDzCiXTRP0eZ63V7L7RxgN9b4nJYFUy_niJt7iUfU7T0IgFdNrcNxxSC20NNO1idShl4Ej_5Fb2HYtsb_RFEbRRpJg3GVQgkO_6tqANUEoUSyimIte_FRYGpZULgBp52oYcbiMvMv4WXnWIANARWMpAkRPwDvQphvtSLIZRoS5dGNQ2tN9j6L3QVO7fJXKiWdIiBrgZXAQs5LwU8-GCZrASiTRnFw2HQnkYMugbe7K5GyiigFMAE',
'kuaishou.server.web_ph': '1add9f8e5ac33f374cfd8dba6cde9f596395',
}
headers = {
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive','Origin': 'https://www.kuaishou.com',
'Referer': 'https://www.kuaishou.com/profile/3xjs47vsmgzxgnu',
'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/103.0.0.0 Safari/537.36',
'accept': '*/*',
'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
# 携带的数据
json_data = {
'operationName': 'visionProfilePhotoList',
'variables': {
'userId': '3xjs47vsmgzxgnu',
'pcursor': '',
'page': 'profile',
},
'query': 'fragment photoContent on PhotoEntity {\n id\n duration\n caption\n likeCount\n viewCount\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 __typename\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 __typename\n }\n canAddComment\n llsid\n status\n currentPcursor\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',
}
response = requests.post('https://www.kuaishou.com/graphql', cookies=cookies, headers=headers, json=json_data)
print(response.json())
第二步:对数据进行提取和解析
for tony in response.json()['data']['visionProfilePhotoList']['feeds']:
print(tony['photo'])
接下来,大家按照这种方式进行数据的提取就可以了 ;视频的地址链接就在该数据中。如果大家不会保存,可以看我前面写的爬虫系列文章,都有讲解如何保存视频文件的。
实践才是检验真理的唯一标准。