文章目录
简单用到两个库:
1.requests
2.json
headers需要三项
1.User-Agent
2.cookie
3.content-type
网页分析
经过分析,可以知道用户基本信息都是从接口api:'https://www.kuaishou.com/graphql'中响应的。是经过post类型传参,payload完整填写时,其中keyword为用户id,赋予一个变量为 search_id = input('请输入快手ID:')。
url = 'https://www.kuaishou.com/graphql'
payload = {"operationName": "graphqlSearchUser",
"variables": {"keyword":search_id},
"query": "query graphqlSearchUser($keyword: String, $pcursor: String, $searchSessionId: String) {\n visionSearchUser(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId) {\n result\n users {\n fansCount\n photoCount\n isFollowing\n user_id\n headurl\n user_text\n user_name\n verified\n verifiedDetail {\n description\n iconType\n newVerified\n musicCompany\n type\n __typename\n }\n __typename\n }\n searchSessionId\n pcursor\n __typename\n }\n}\n"
}
将这些数据转换为json格式,用requests.post的方法把数据传参进去,再返回json数据。
payload_data = json.dumps(payload)
resp = requests.post(url, data=payload_data, headers=headers).json()
将返回的数据做整理。
完整代码展示
import requests
import json
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
'cookie': ,#自行输入cookie
'content-type': 'application/json',
}
searchapi = 'https://www.kuaishou.com/search/author?searchKey='
search_id = input('请输入快手ID:')
url = 'https://www.kuaishou.com/graphql'
payload = {"operationName": "graphqlSearchUser",
"variables": {"keyword":search_id},
"query": "query graphqlSearchUser($keyword: String, $pcursor: String, $searchSessionId: String) {\n visionSearchUser(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId) {\n result\n users {\n fansCount\n photoCount\n isFollowing\n user_id\n headurl\n user_text\n user_name\n verified\n verifiedDetail {\n description\n iconType\n newVerified\n musicCompany\n type\n __typename\n }\n __typename\n }\n searchSessionId\n pcursor\n __typename\n }\n}\n"
}
payload_data = json.dumps(payload)
resp = requests.post(url, data=payload_data, headers=headers).json()
#print(resp)
uid = resp['data']['visionSearchUser']['users'][0]['user_id']
name = resp['data']['visionSearchUser']['users'][0]['user_name']
user_data = resp['data']['visionSearchUser']['users'][0]
#print(url_id, name)
def verified():
verified = user_data['verified']
if verified == 'true':
return (user_data['verifiedDetail']['description'])
else:
return ('无')
def user_text():
user_text = user_data['user_text']
if user_text == '':
return '暂无签名'
else:
return user_text
gourl = 'https://www.kuaishou.com/profile/' + uid
payload_2 = {"operationName":"visionProfile",
"variables":{"userId":uid},
"query":"query visionProfile($userId: String) {\n visionProfile(userId: $userId) {\n result\n hostName\n userProfile {\n ownerCount {\n fan\n photo\n follow\n photo_public\n __typename\n }\n profile {\n gender\n user_name\n user_id\n headurl\n user_text\n user_profile_bg_url\n __typename\n }\n isFollowing\n __typename\n }\n __typename\n }\n}\n"
}
payload_2_data = json.dumps(payload_2)
resp_2 = requests.post(url, data=payload_2_data, headers=headers).json()
#print(resp_2)
userProfile = resp_2['data']['visionProfile']['userProfile']
def gender():
gender = userProfile['profile']['gender']
if gender == 'M':
return ('男')
if gender == 'F':
return ('女')
data = {
'用户ID': uid,
'昵称': name,
'性别': gender(),
'签名': user_text(),
'作品': userProfile['ownerCount']['photo_public'],
'粉丝': userProfile['ownerCount']['fan'],
'关注': userProfile['ownerCount']['follow'],
'认证': verified(),
'头像': userProfile['profile']['headurl'],
}
print(data)
return data