SpiderPromoterHeader.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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; soft_did=1619580708547; didv=1674986117437; kpf=PC_WEB; clientid=3; did=web_6cdc432ef91373345c3b4f1b16559976; apdid=87629ef8-112a-405e-aa68-6532dad20945f1aacbdb5b9379d0b02fa529ecbc80c2:1681378698:1; userId=1433645207; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABO7d6hu9qxJCzWjVA62hSp9zWXg9SCNBU45lpRe7kz0KnW7FZL4QvULoBerxFtRIZceE96YcmTFvXPVu02rKu_IhCjfE5toca2N1Hxq6NNsk42T2s1U48JjbYe-48lzAOp0SU3G9wM6zSeTNcBwVJVZ-FL5u5Je0j9gzWo4gxhKIuRgFY37kWOpa7uG9JINrjynHlOhHm2Qrs_6HZHk-4ahoSWXnZQFypWC8Fi7687FtZGgfDIiCPf5NE2AVLBwwduWgffV0fhRYpMDq9izf6QN8O_7b0CCgFMAE; kuaishou.server.web_ph=cdd0202e2c6cbffc6c4425d8534a070a67c1; kpn=KUAISHOU_VISION',
  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. print(response.text, '--------text-------------')
  35. try:
  36. if json.loads(response.text) is not None:
  37. data = json.loads(response.text)["data"]
  38. if data is not None:
  39. visionSearchUser = data["visionSearchUser"]
  40. if visionSearchUser is not None:
  41. userList = visionSearchUser["users"]
  42. if userList is not None:
  43. List = []
  44. for user in userList:
  45. user_id = user["user_id"]
  46. List.append(user_id)
  47. return List
  48. else:
  49. pass
  50. except Exception as e:
  51. print('获取用户ID错误', response.text)
  52. @staticmethod
  53. def GetPromoterInfo(promoter_id):
  54. List = SpiderPromoterHeader.GetUserID(promoter_id)
  55. listItem = []
  56. for user_id in List:
  57. url = "https://www.kuaishou.com/graphql"
  58. payload = json.dumps({
  59. "operationName": "visionProfile",
  60. "variables": {
  61. "userId": user_id
  62. },
  63. "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"
  64. })
  65. headers = {
  66. 'Cookie': 'clientid=3; client_key=65890b29; soft_did=1619580708547; didv=1674986117437; kpf=PC_WEB; clientid=3; did=web_6cdc432ef91373345c3b4f1b16559976; apdid=87629ef8-112a-405e-aa68-6532dad20945f1aacbdb5b9379d0b02fa529ecbc80c2:1681378698:1; userId=1433645207; kpn=KUAISHOU_VISION; kuaishou.server.web_st=ChZrdWFpc2hvdS5zZXJ2ZXIud2ViLnN0EqABJmId7nrMbNwNCAHx1plSfa7wKs8OKhC3Sh7yNwmIUAgRDb0NpoLqRQbvI4wLa-2oGiq5jAT0X4ZVgIcC1VkicZ4nfCqMoOaP_ZdJxTXtr1okc0NLqVbArL_daeHhvFs9foIyhppgogJzQIWmEelbFhy66FtfTrpPIDDU4U3iE68XzKJ3qQBPiTiRSqdybKLbiG7g4dIOJEcaR-uRWz2I7RoSTdCMiCqspRXB3AhuFugv61B-IiD_sF73q-l2oDD0TV2N22UFgv4yH9grr9eTElLXKz2tjSgFMAE; kuaishou.server.web_ph=f30c9f58ebbab9e7949f3bcf778585b81a3e',
  67. 'User-Agent': 'Apifox/1.0.0 (https://www.apifox.cn)',
  68. 'Content-Type': 'application/json',
  69. 'Accept': '*/*',
  70. 'Host': 'www.kuaishou.com',
  71. 'Connection': 'keep-alive'
  72. }
  73. try:
  74. response = requests.request("POST", url, headers=headers, data=payload)
  75. data = json.loads(response.text)["data"]
  76. if data is not None:
  77. visionProfile = data["visionProfile"]
  78. if visionProfile is not None:
  79. userProfile = visionProfile["userProfile"]
  80. if userProfile is not None:
  81. fans = userProfile["ownerCount"]["fan"]
  82. headurl = userProfile["profile"]["headurl"]
  83. user_name = userProfile["profile"]["user_name"]
  84. item = {"fans": fans, "handurl": headurl, "promoter_name": user_name,
  85. "promoter_id": promoter_id}
  86. listItem.append(item)
  87. print(listItem)
  88. return listItem
  89. else:
  90. continue
  91. except Exception as e:
  92. print('请求达人详情异常', e)
  93. def GetPromoterID(self):
  94. try:
  95. sql = """select a.promoter_id from ruixuan.promoter_oerder_amount a
  96. left join ruixuan.promoter_info b
  97. on a.promoter_id=b.promoter_id
  98. where b.promoter_id is null and a.`30day_gmv` is not null
  99. group by a.promoter_id"""
  100. promoterIdList = self.conn.QueryAll(sql)
  101. return promoterIdList
  102. except Exception as e:
  103. print(e)
  104. def PromoterInsert(self):
  105. promoterIdList = self.GetPromoterID()
  106. try:
  107. for promoterId in promoterIdList:
  108. print(promoterId)
  109. try:
  110. listItem = SpiderPromoterHeader.GetPromoterInfo(promoterId[0])
  111. if listItem is not None:
  112. if listItem.__len__() > 0:
  113. for item in listItem:
  114. try:
  115. self.conn.insert(item)
  116. except Exception as e:
  117. print('插入错误', item)
  118. except Exception as e:
  119. print(e)
  120. except Exception as e:
  121. print('获取达人详情错误', e)
  122. if __name__ == '__main__':
  123. SpiderPromoterHeader().PromoterInsert()