# 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 random import time import redis import requests from utils.cookie_update import cookie_update from utils.mysql_utils_pro import MysqlProUtils from utils.send_feishu_msg import SendFeiShuMsg class SpiderPromoterHeader: def __init__(self): self.conn = MysqlProUtils() @staticmethod def GetPromoterInfo(promoter_id): r = redis.Redis(host='192.168.0.99', password='', port=6379, db=0) listItem = [] url = 'https://cps.kwaixiaodian.com/distribute/pc/seller/promoter/info?promoterId={promoterId}&type=1'.format( promoterId=promoter_id) if r.zcard('kuaishou_shop_click_token') == 0: cookie_update.get_click_coookie_to_redis() else: random_member = r.zrange('kuaishou_shop_click_token', 0, -1, withscores=True) random_item = random.choice(random_member) # cookie = random_item[0].decode('utf-8') cookie = cookie_update.get_cookie_handler() phone_num = int(random_item[1]) headers = {'User-Agent': 'Mozilla/5.0', 'Cookie': cookie} try: rep = requests.get(url=url, headers=headers) print(rep.text, '-----rep.txt---------') # if json.loads(rep.text)["result"] != 1 and json.loads(rep.text)["result"] != 100110000: # cookie_update.update_temporary_cookie_handler(phone_num) # retry_cookie = cookie_update.get_cookie_handler()[0] # headers = {'User-Agent': 'Mozilla/5.0', # 'Cookie': retry_cookie} # rep = requests.get(url=url, headers=headers) # data = json.loads(rep.text)["data"] # item = {"fans": data["fanNum"], "handurl": data["promoterHeadImgUrl"], # "promoter_name": data["promoterNickName"], # "promoter_id": promoter_id} # listItem.append(item) # return listItem # elif json.loads(rep.text)["result"] == 100110000: # cookie_update.update_cookie_handler(phone_num) # return if json.loads(rep.text)["result"] == 1: data = json.loads(rep.text)["data"] print(data) item = {"fans": data["fanNum"], "handurl": data["promoterHeadImgUrl"], "promoter_name": data["promoterNickName"], "promoter_id": promoter_id} listItem.append(item) return listItem else: return except Exception as e: SendFeiShuMsg.send_robot_msg( 'GetPromoterInfo {promoterId}请求错误请检查cookie{e}'.format(promoterId=promoter_id, e=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: print(promoterId) try: listItem = SpiderPromoterHeader.GetPromoterInfo(int(promoterId[0])) time.sleep(60) 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) except Exception as e: print('获取达人详情错误', e) if __name__ == '__main__': SpiderPromoterHeader().PromoterInsert()