# Author renyupeng # coding=utf-8 # @Time : 2023/3/23 2:49 下午 # @Site : # @File : SpiderPromoterHeader.py # @Software: PyCharm # @contact: renyupeng@c-top.com.cn # @Tel 1501435553 import json import requests from utils.mysql_utils_pro import MysqlProUtils class SpiderPromoterHeader: def __init__(self): self.conn = MysqlProUtils() @staticmethod def GetUserID(promoter_id): url = "https://www.kuaishou.com/graphql" payload = json.dumps({ "operationName": "graphqlSearchUser", "variables": { "keyword": "{promoter_id}".format(promoter_id=promoter_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" }) headers = { 'Cookie': 'clientid=3; client_key=65890b29; apdid=87629ef8-112a-405e-aa68-6532dad20945f1aacbdb5b9379d0b02fa529ecbc80c2:1655966300:1; soft_did=1619580708547; didv=1674986117437; userId=562684945; kpf=PC_WEB; clientid=3; did=web_6cdc432ef91373345c3b4f1b16559976; language=zh-CN; kpn=KUAISHOU_VISION; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABZCgiwUU-KZ-8L43M2naDgEVSKj3xcq87d9nPQarkykdVN5cfPtggvNZLlGK3a1fwnNNPzisjAWck5h_n8Iq3a-MKtz-GWQBLk7pck7DyIs8kvuRfDyEOOkMxJMJDQ_7nStQABCRRNpuIoGzR0w6V6sY2LAHoeFttrU6W8J8bfSfMfVh4pbYHGfYw0flDP5XedFKP3JNDuQmRyvWIlgV9XRoSTdCMiCqspRXB3AhuFugv61B-IiCzxP1plSgycooFn_D4LshJZBLKD6p2rW3cvGu0ZxDSpygFMAE; kuaishou.server.web_ph=69cb6d81113492600e71f8e2242b196f4b36', 'User-Agent': 'Apifox/1.0.0 (https://www.apifox.cn)', 'Content-Type': 'application/json', 'Accept': '*/*', 'Host': 'www.kuaishou.com', 'Connection': 'keep-alive' } response = requests.request("POST", url, headers=headers, data=payload) try: if json.loads(response.text) is not None: data = json.loads(response.text)["data"] if data is not None: visionSearchUser = data["visionSearchUser"] if visionSearchUser is not None: userList = visionSearchUser["users"] if userList is not None: List = [] for user in userList: user_id = user["user_id"] List.append(user_id) return List else: pass except Exception as e: print('获取用户ID错误', response.text) @staticmethod def GetPromoterInfo(promoter_id): List = SpiderPromoterHeader.GetUserID(promoter_id) listItem = [] for user_id in List: url = "https://www.kuaishou.com/graphql" payload = json.dumps({ "operationName": "visionProfile", "variables": { "userId": user_id }, "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" }) headers = { 'Cookie': 'clientid=3; client_key=65890b29; apdid=87629ef8-112a-405e-aa68-6532dad20945f1aacbdb5b9379d0b02fa529ecbc80c2:1655966300:1; soft_did=1619580708547; didv=1674986117437; userId=562684945; kpf=PC_WEB; clientid=3; did=web_6cdc432ef91373345c3b4f1b16559976; language=zh-CN; kpn=KUAISHOU_VISION; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABZCgiwUU-KZ-8L43M2naDgEVSKj3xcq87d9nPQarkykdVN5cfPtggvNZLlGK3a1fwnNNPzisjAWck5h_n8Iq3a-MKtz-GWQBLk7pck7DyIs8kvuRfDyEOOkMxJMJDQ_7nStQABCRRNpuIoGzR0w6V6sY2LAHoeFttrU6W8J8bfSfMfVh4pbYHGfYw0flDP5XedFKP3JNDuQmRyvWIlgV9XRoSTdCMiCqspRXB3AhuFugv61B-IiCzxP1plSgycooFn_D4LshJZBLKD6p2rW3cvGu0ZxDSpygFMAE; kuaishou.server.web_ph=69cb6d81113492600e71f8e2242b196f4b36', 'User-Agent': 'Apifox/1.0.0 (https://www.apifox.cn)', 'Content-Type': 'application/json', 'Accept': '*/*', 'Host': 'www.kuaishou.com', 'Connection': 'keep-alive' } try: response = requests.request("POST", url, headers=headers, data=payload) data = json.loads(response.text)["data"] if data is not None: visionProfile = data["visionProfile"] if visionProfile is not None: userProfile = visionProfile["userProfile"] if userProfile is not None: fans = userProfile["ownerCount"]["fan"] headurl = userProfile["profile"]["headurl"] user_name = userProfile["profile"]["user_name"] item = {"fans": fans, "handurl": headurl, "promoter_name": user_name, "promoter_id": promoter_id} listItem.append(item) return listItem else: continue except Exception as e: print('请求达人详情异常', e) def GetPromoterID(self): try: sql = """select a.promoter_id from ruixuan.promoter_oerder_amount a left join ruixuan.promoter_info b on a.promoter_id=b.promoter_id where b.promoter_id is null and a.`30day_gmv` is not null group by a.promoter_id""" promoterIdList = self.conn.QueryAll(sql) return promoterIdList except Exception as e: print(e) def PromoterInsert(self): promoterIdList = self.GetPromoterID() try: for promoterId in promoterIdList: listItem = SpiderPromoterHeader.GetPromoterInfo(promoterId[0]) if listItem is not None: if listItem.__len__() > 0: for item in listItem: try: self.conn.insert(item) except Exception as e: print('插入错误', item) except Exception as e: print('获取达人详情错误', e) if __name__ == '__main__': SpiderPromoterHeader().PromoterInsert()