| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | # Author renyupeng# coding=utf-8# @Time    : 2023/3/23 2:49 下午# @Site    : # @File    : SpiderPromoterHeaderYufu.py# @Software: PyCharm# @contact: renyupeng@c-top.com.cn# @Tel 1501435553import jsonimport randomimport timeimport redisimport requestsfrom utils.cookie_update import cookie_updatefrom utils.mysql_utils_pro import MysqlProUtilsfrom utils.send_feishu_msg import SendFeiShuMsgclass SpiderPromoterHeaderYufu:    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()[0]            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 yufu.promoter_oerder_amount a    left join yufu.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 = SpiderPromoterHeaderYufu.GetPromoterInfo(int(promoterId[0]))                    time.sleep(60)                    if listItem is not None:                        if listItem.__len__() > 0:                            for item in listItem:                                try:                                    self.conn.yufuInsert(item)                                except Exception as e:                                    print('插入错误', item)                except Exception as e:                    print(e)        except Exception as e:            print('获取达人详情错误', e)if __name__ == '__main__':    SpiderPromoterHeaderYufu().PromoterInsert()
 |