SpiderPromoterHeader.py 7.3 KB

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