SpiderPromoterHeader.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2023/3/23 2:49 下午
  4. # @Site :
  5. # @File : SpiderPromoterHeader.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import json
  10. import requests
  11. from utils.mysql_utils_pro import MysqlProUtils
  12. class SpiderPromoterHeader:
  13. def __init__(self):
  14. self.conn = MysqlProUtils()
  15. @staticmethod
  16. def GetUserID(promoter_id):
  17. url = "https://www.kuaishou.com/graphql"
  18. payload = json.dumps({
  19. "operationName": "graphqlSearchUser",
  20. "variables": {
  21. "keyword": "{promoter_id}".format(promoter_id=promoter_id)
  22. },
  23. "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"
  24. })
  25. headers = {
  26. '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',
  27. 'User-Agent': 'Apifox/1.0.0 (https://www.apifox.cn)',
  28. 'Content-Type': 'application/json',
  29. 'Accept': '*/*',
  30. 'Host': 'www.kuaishou.com',
  31. 'Connection': 'keep-alive'
  32. }
  33. response = requests.request("POST", url, headers=headers, data=payload)
  34. try:
  35. if json.loads(response.text) is not None:
  36. data = json.loads(response.text)["data"]
  37. if data is not None:
  38. visionSearchUser = data["visionSearchUser"]
  39. if visionSearchUser is not None:
  40. userList = visionSearchUser["users"]
  41. if userList is not None:
  42. List = []
  43. for user in userList:
  44. user_id = user["user_id"]
  45. List.append(user_id)
  46. return List
  47. else:
  48. pass
  49. except Exception as e:
  50. print('获取用户ID错误', response.text)
  51. @staticmethod
  52. def GetPromoterInfo(promoter_id):
  53. List = SpiderPromoterHeader.GetUserID(promoter_id)
  54. listItem = []
  55. for user_id in List:
  56. url = "https://www.kuaishou.com/graphql"
  57. payload = json.dumps({
  58. "operationName": "visionProfile",
  59. "variables": {
  60. "userId": user_id
  61. },
  62. "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"
  63. })
  64. headers = {
  65. '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',
  66. 'User-Agent': 'Apifox/1.0.0 (https://www.apifox.cn)',
  67. 'Content-Type': 'application/json',
  68. 'Accept': '*/*',
  69. 'Host': 'www.kuaishou.com',
  70. 'Connection': 'keep-alive'
  71. }
  72. try:
  73. response = requests.request("POST", url, headers=headers, data=payload)
  74. data = json.loads(response.text)["data"]
  75. if data is not None:
  76. visionProfile = data["visionProfile"]
  77. if visionProfile is not None:
  78. userProfile = visionProfile["userProfile"]
  79. if userProfile is not None:
  80. fans = userProfile["ownerCount"]["fan"]
  81. headurl = userProfile["profile"]["headurl"]
  82. user_name = userProfile["profile"]["user_name"]
  83. item = {"fans": fans, "handurl": headurl, "promoter_name": user_name,
  84. "promoter_id": promoter_id}
  85. listItem.append(item)
  86. return listItem
  87. else:
  88. continue
  89. except Exception as e:
  90. print('请求达人详情异常', e)
  91. def GetPromoterID(self):
  92. try:
  93. sql = """select a.promoter_id from ruixuan.promoter_oerder_amount a
  94. left join ruixuan.promoter_info b
  95. on a.promoter_id=b.promoter_id
  96. where b.promoter_id is null and a.`30day_gmv` is not null
  97. group by a.promoter_id"""
  98. promoterIdList = self.conn.QueryAll(sql)
  99. return promoterIdList
  100. except Exception as e:
  101. print(e)
  102. def PromoterInsert(self):
  103. promoterIdList = self.GetPromoterID()
  104. try:
  105. for promoterId in promoterIdList:
  106. listItem = SpiderPromoterHeader.GetPromoterInfo(promoterId[0])
  107. if listItem is not None:
  108. if listItem.__len__() > 0:
  109. for item in listItem:
  110. try:
  111. self.conn.insert(item)
  112. except Exception as e:
  113. print('插入错误', item)
  114. except Exception as e:
  115. print('获取达人详情错误', e)
  116. if __name__ == '__main__':
  117. SpiderPromoterHeader().PromoterInsert()