自动优化


import xlwt
import xlrd
import json
import requests
import time
import copy
from selenium import webdriver


class adReport():
    def __init__(self):

        print('************************************************************************')
        self.chooseModel = int(input('1. 优化      2.百分比调价 :'))
        if self.chooseModel == 2:
            percent = float(input('需要整体调整到当前出价的多少倍(如 1.2, 0.8):'))
        self.getConf()
        self.getSession()

        session_list = [self.session, self.session1, self.session2, self.session3, self.session4]
        i = 1
        if self.chooseModel == 1:
            while True:
                try:
                    self.getData()
                    print('基础数据下载完成!')
                    self.getPrice()
                    print('价格下载完成!')
                    self.analysisData()
                    self.mergeData()
                    self.result = 'ok'
                    break
                except:
                    if i <4 :
                        time.sleep(2)
                        self.session = session_list[i]
                        i += 1
                        print('因网络波动,部分数据缺失,正在重新获取数据..')
                    else:
                        print('网速较差,建议重启程序。')
                        self.result = 'defeat'
                        break
        else:
            while True:
                try:
                    self.getData()
                    print('基础数据下载完成!')
                    self.getPrice()
                    print('价格下载完成!')
                    self.analysisData()
                    self.percentAdjust(percent)
                    self.result = 'ok'
                    break
                except:
                    if i <4 :
                        time.sleep(2)
                        self.session = session_list[i]
                        i += 1
                        print('因网络波动,价格数据缺失,正在重新获取价格..')
                    else:
                        print('网速较差,建议重启程序。')
                        self.result = 'defeat'
                        break


    def getConf(self):
        self.beginTime = input('请输入项目开始日期(格式如2021-01-01):')
        self.endTime = input('请输入数据截止日期(格式如2021-01-01):')
        self.duration = input('效果时长 :1.当天   2. 15天  :')
        self.order = input('订单维度:1. 下单   2. 成交  :')
        # self.beginTime = '2020-12-17'
        # self.endTime = '2020-12-31'
        # self.duration = 2
        # self.order = 2


    def getSession(self):

        print('1. 京牌代理界面      2. 京准通界面')
        chooseUrl = input('请输入您的选择:')
        browser = webdriver.Chrome()
        browser.implicitly_wait(20)  # 全局等待20s
        if chooseUrl == 1:
            browser.get('https://agency.jd.com/')
        else:
            browser.get('https://jzt.jd.com/gw/')

        while True:
            if browser.title == '京准通首页':
                time.sleep(4)
                break

        sel_cookies = browser.get_cookies()  # 获取selenium侧的cookies
        browser.quit()

        jar = requests.cookies.RequestsCookieJar()  # 先构建RequestsCookieJar对象
        for i in sel_cookies:
            # 将selenium侧获取的完整cookies的每一个cookie名称和值传入RequestsCookieJar对象
            # domain和path为可选参数,主要是当出现同名不同作用域的cookie时,为了防止后面同名的cookie将前者覆盖而添加的
            jar.set(i['name'], i['value'], domain=i['domain'], path=i['path'])

        self.session = requests.session()  # requests以session会话形式访问网站
        self.session.cookies.update(jar)  # 将配置好的RequestsCookieJar对象加入到requests形式的session会话中
        self.session1 = requests.session()
        self.session1.cookies.update(jar)
        self.session2 = requests.session()
        self.session2.cookies.update(jar)
        self.session3 = requests.session()
        self.session3.cookies.update(jar)
        self.session4 = requests.session()
        self.session4.cookies.update(jar)

    def getData(self):
        url = 'https://zt.jd.com/fcgi-bin/jdzt/exportreport/exportadcomprpt?g_tk=415270190&g_ty=ajax'
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
        status = ['启动中', '暂停中']
        data = []

        for i in range(2):
            data1 = {
                'delflag': '2',
                'mediatype': '0',
                'begindate': '%s' % self.beginTime,
                'enddate': '%s' % self.endTime,
                'totaltimetype': '1',
                'durationtype': '%s' % self.duration,
                'orderstatus': '%s' % self.order,
                'bonusordertype': '2',
                'ie': 'utf-8',
                'orderbyitem': '0',
                'orderbyorder': '0',
                'adtitle': '',
                'adid': '',
                'planid': '',
                'adstatus': '%d' % (i+1),
                'specid': '0',
            }
            time.sleep(2)
            req = self.session.post(url, headers=headers, data=data1)

            tem_list = req.text.strip().split('\r\n')
            col_tem= tem_list[8].split(',')
            datas = tem_list[9::]

            for j in range(len(datas)):
                tem_list = datas[j].replace('"', "").split(',')
                tem_list.insert(1, status[i])
                data.append(tem_list)

        col_index = col_tem
        col_index.insert(1, '状态')
        try:
            data.insert(0, col_index)
            self.df = data
        except (ValueError):
            pass

        for i in range(len(col_index)):
            if col_index[i] == '推广计划ID':
                self.planId_col = i
                break
        planId_list = []
        self.adId_list = []
        for i in range(1, len(self.df)):
            planId_list.append(self.df[i][self.planId_col])
            self.adId_list.append(self.df[i][0])
        tem_planId = list(set(planId_list))
        planId_cnt = [tem_planId, []]
        for i in planId_cnt[0]:
            planId_cnt[1].append(planId_list.count(i))
        self.planId_cnt = planId_cnt


    def getPrice(self):
        self.price = {}
        self.mediaRefer = {
            1: '腾讯',
            2: '头条',
            4: '百度',
            5: '京x'
        }
        url = 'https://zt.jd.com/fcgi-bin/jdzt/ad/list_v2?ie=utf-8&g_tk=1456803582&g_ty=undefined'
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
        for i in range(len(self.planId_cnt[0])):
            n_page = self.planId_cnt[1][i] // 100 + 3
            planId = self.planId_cnt[0][i]

            for j in range(1, n_page):
                data = {
                    'profileId': '0',
                    'planId': planId,
                    'adSpecIdList': '',
                    'adState': '0',
                    'mediaType': '0',
                    'bidType': '0',
                    'flowPacketId': '',
                    'paginator': '{"pageIndex":%d,"pageSize":100}' % j
                }
                while True:
                    try:
                        req = self.session.post(url, headers = headers, data = data)
                        total_list = json.loads(req.text)['data']['list']

                        for k in range(len(total_list)):
                            adId = total_list[k]['adId']
                            tCpa = float(total_list[k]['tcpaPrice'])
                            click = float(total_list[k]['clickPrice'])
                            adType = int(total_list[k]['bidType'])
                            adMedia = int(total_list[k]['mediaType'])
                            if tCpa == 0:
                                self.price[adId] = [click, adType, self.mediaRefer[adMedia]]
                            else:
                                self.price[adId] = [tCpa, adType, self.mediaRefer[adMedia]]
                        break
                    except TimeoutError:
                        time.sleep(1)
                        print('[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。')
                    except Exception as e:
                        print(repr(e))
                        break


    def againPrice(self, adId):
        self.price = {}
        url = 'https://zt.jd.com/fcgi-bin/jdzt/ad/list_v2?ie=utf-8&g_tk=503153523&g_ty=undefined'
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
        data = {
            'profileId': '0',
            'planId': '0',
            'adId': adId,
            'adSpecIdList': '',
            'adState': '0',
            'mediaType': '0',
            'bidType': '0',
            'flowPacketId': '',
            'paginator': '{"pageIndex":1,"pageSize":100}'
        }
        req = self.session1.post(url, headers=headers, data=data)
        while True:
            try:
                time.sleep(2)
                total_list = json.loads(req.text)['data']['list'][0]
                tCpa = float(total_list['tcpaPrice'])
                click = float(total_list['clickPrice'])
                adType = int(total_list['bidType'])
                adMedia = int(total_list['mediaType'])
                break
            except IndexError:
                req = self.session2.post(url, headers=headers, data=data)
                print(adId)
                pass
        if tCpa > 0:
            return [tCpa, adType, self.mediaRefer[adMedia]]
        else:
            return [click, adType, self.mediaRefer[adMedia]]


    def analysisData(self):
        for i in range(len(self.df[0])):
            if self.df[0][i] == '广告消耗':
                self.cost_col = i
            elif self.df[0][i] == 'ROI':
                self.roi_col = i
                break
            elif self.df[0][i] == '订单金额':
                self.amount_col = i


        tem_roi = []
        tem_cost = []
        tem_planId = copy.deepcopy(self.planId_cnt[0])
        tem_plannum = copy.deepcopy(self.planId_cnt[1])
        for i in range(len(self.planId_cnt[0])):
            cost_sum = 0
            amount_sum = 0
            for j in range(1, len(self.df)):
                if self.df[j][3] == tem_planId[i]:
                    cost_sum += float(self.df[j][self.cost_col])
                    amount_sum += float(self.df[j][self.amount_col])
            if cost_sum == 0:
                self.planId_cnt[0].remove(tem_planId[i])
                self.planId_cnt[1].remove(tem_plannum[i])
            else:
                tem_roi.append(round(amount_sum/cost_sum, 2))
                tem_cost.append(round(cost_sum/tem_plannum[i], 2))
        self.planId_cnt.append(tem_roi)
        self.planId_cnt.append(tem_cost)


    def mergeData(self):
        price_list = []
        type_list = []
        media_list = []
        for i in self.adId_list:
            price_list.append(self.price[i][0])
            type_list.append(self.price[i][1])
            media_list.append(self.price[i][2])


        self.df[0].insert(0, '渠道')
        self.df[0].insert(2, '现在出价')
        self.df[0].insert(3, '出价类型')
        for i in range(1, len(self.df)):
            self.df[i].insert(0, media_list[i - 1])
            self.df[i].insert(2, price_list[i-1])
            self.df[i].insert(3, type_list[i-1])
        workbook = xlwt.Workbook(encoding='utf-8')
        worksheet1 = workbook.add_sheet('Sheet1')
        worksheet2 = workbook.add_sheet('Sheet2')
        for i in range(len(self.df)):
            for j in range(len(self.df[0])):
                worksheet1.write(i, j, self.df[i][j])
        sheet2_colname = ['渠道', '广告ID', '更改为', '出价模式', '状态更改', '计划名称', '计划内广告平均消耗', '计划ROI', '现在出价', '现在消耗', '现在ROI']
        for i in range(len(sheet2_colname)):
            worksheet2.write(0, i, sheet2_colname[i])
        self.planId_col = 3 + self.planId_col
        self.cost_col = 3 + self.cost_col
        self.roi_col = 3 + self.roi_col
        change_num = 1
        style1 = xlwt.XFStyle()
        font1 = xlwt.Font()  # 创建一个文本格式,包括字体、字号和颜色样式特性
        font1.colour_index = 3 # 设置其字体颜色
        font1.bold = True
        style1.font = font1  # 将赋值好的模式参数导入Style
        style2 = xlwt.XFStyle()
        font2 = xlwt.Font()  # 创建一个文本格式,包括字体、字号和颜色样式特性
        font2.colour_index = 2 # 设置其字体颜色
        font2.bold = True
        style2.font = font2  # 将赋值好的模式参数导入Style
        for i in range(1, len(self.df)):
            if self.df[i][self.planId_col] in self.planId_cnt[0]:
                plan_index = self.planId_cnt[0].index(self.df[i][self.planId_col])
                if float(self.df[i][self.roi_col]) > self.planId_cnt[2][plan_index]*2:
                    if self.df[i][4] == '暂停中':
                        worksheet2.write(change_num, 4, '开启', style1)
                    worksheet2.write(change_num, 0, self.df[i][0])
                    worksheet2.write(change_num, 1, int(self.df[i][1]))
                    if self.df[i][2]<0.5:
                        worksheet2.write(change_num, 2, self.df[i][2] + 0.2, style1)
                    else:
                        worksheet2.write(change_num, 2, round(self.df[i][2]*1.3, 1), style1)
                    worksheet2.write(change_num, 3, self.df[i][3])
                    worksheet2.write(change_num, 5, self.df[i][7])
                    worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                    worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                    worksheet2.write(change_num, 8, self.df[i][2])
                    worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                    worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                    change_num += 1
                elif float(self.df[i][self.roi_col]) > self.planId_cnt[2][plan_index]*1.5:
                    if self.df[i][4] == '暂停中':
                        worksheet2.write(change_num, 4, '开启', style1)
                    worksheet2.write(change_num, 0, self.df[i][0])
                    worksheet2.write(change_num, 1, int(self.df[i][1]))
                    if self.df[i][2]<0.5:
                        worksheet2.write(change_num, 2, self.df[i][2] + 0.2, style1)
                    else:
                        worksheet2.write(change_num, 2, round(self.df[i][2]*1.2, 1), style1)
                    worksheet2.write(change_num, 3, self.df[i][3])
                    worksheet2.write(change_num, 5, self.df[i][7])
                    worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                    worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                    worksheet2.write(change_num, 8, self.df[i][2])
                    worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                    worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                    change_num += 1
                elif float(self.df[i][self.roi_col]) > self.planId_cnt[2][plan_index]*1.2:
                    if self.df[i][4] == '暂停中':
                        worksheet2.write(change_num, 4, '开启', style1)
                    worksheet2.write(change_num, 0, self.df[i][0])
                    worksheet2.write(change_num, 1, int(self.df[i][1]))
                    if self.df[i][2] < 0.5:
                        worksheet2.write(change_num, 2, self.df[i][2] + 0.1, style1)
                    else:
                        worksheet2.write(change_num, 2, round(self.df[i][2] * 1.2, 1), style1)
                    worksheet2.write(change_num, 3, self.df[i][3])
                    worksheet2.write(change_num, 5, self.df[i][7])
                    worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                    worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                    worksheet2.write(change_num, 8, self.df[i][2])
                    worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                    worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                    change_num += 1
                elif float(self.df[i][self.roi_col]) < self.planId_cnt[2][plan_index]*0.85 and float(self.df[i][self.cost_col]) > self.planId_cnt[3][plan_index]*0.8:
                    if self.df[i][4] == '暂停中':
                        worksheet2.write(change_num, 4, '开启', style1)
                    worksheet2.write(change_num, 0, self.df[i][0])
                    worksheet2.write(change_num, 1, int(self.df[i][1]))
                    worksheet2.write(change_num, 2, round(self.df[i][2]*0.9, 1), style2)
                    worksheet2.write(change_num, 3, self.df[i][3])
                    worksheet2.write(change_num, 5, self.df[i][7])
                    worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                    worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                    worksheet2.write(change_num, 8, self.df[i][2])
                    worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                    worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                    change_num += 1
                elif float(self.df[i][self.roi_col]) < self.planId_cnt[2][plan_index]*0.7 and float(self.df[i][self.cost_col]) > self.planId_cnt[3][plan_index]*0.8:
                    if self.df[i][4] == '暂停中':
                        worksheet2.write(change_num, 4, '开启', style1)
                    worksheet2.write(change_num, 0, self.df[i][0])
                    worksheet2.write(change_num, 1, int(self.df[i][1]))
                    worksheet2.write(change_num, 2, round(self.df[i][2]*0.8, 1), style2)
                    worksheet2.write(change_num, 3, self.df[i][3])
                    worksheet2.write(change_num, 5, self.df[i][7])
                    worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                    worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                    worksheet2.write(change_num, 8, self.df[i][2])
                    worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                    worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                    change_num += 1
                elif float(self.df[i][self.roi_col]) < self.planId_cnt[2][plan_index]*0.5 and float(self.df[i][self.cost_col]) > self.planId_cnt[3][plan_index]*0.8:
                    if self.df[i][4] == '暂停中':
                        worksheet2.write(change_num, 4, '开启', style1)
                    worksheet2.write(change_num, 0, self.df[i][0])
                    worksheet2.write(change_num, 1, int(self.df[i][1]))
                    worksheet2.write(change_num, 2, round(self.df[i][2]*0.7, 1), style2)
                    worksheet2.write(change_num, 3, self.df[i][3])
                    worksheet2.write(change_num, 5, self.df[i][7])
                    worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                    worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                    worksheet2.write(change_num, 8, self.df[i][2])
                    worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                    worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                    change_num += 1
                elif float(self.df[i][self.roi_col]) < self.planId_cnt[2][plan_index]*0.33 and float(self.df[i][self.cost_col]) > self.planId_cnt[3][plan_index]*0.8:
                    if self.df[i][4] == '启动中':
                        worksheet2.write(change_num, 4, '暂停', style2)
                    worksheet2.write(change_num, 0, self.df[i][0])
                    worksheet2.write(change_num, 1, int(self.df[i][1]))
                    worksheet2.write(change_num, 2, self.df[i][2])
                    worksheet2.write(change_num, 3, self.df[i][3])
                    worksheet2.write(change_num, 5, self.df[i][7])
                    worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                    worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                    worksheet2.write(change_num, 8, self.df[i][2])
                    worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                    worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                    change_num += 1



        workbook.save('广告报表.xls')

        print('下载完成!优化意见在sheet2当中,如需调整请直接在sheet2里调整。')
        time.sleep(1.5)


    def percentAdjust(self, percent):
        price_list = []
        type_list = []
        media_list = []
        for i in self.adId_list:
            price_list.append(self.price[i][0])
            type_list.append(self.price[i][1])
            media_list.append(self.price[i][2])

        self.df[0].insert(0, '渠道')
        self.df[0].insert(2, '现在出价')
        self.df[0].insert(3, '出价类型')
        for i in range(1, len(self.df)):
            self.df[i].insert(0, media_list[i - 1])
            self.df[i].insert(2, price_list[i - 1])
            self.df[i].insert(3, type_list[i - 1])
        workbook = xlwt.Workbook(encoding='utf-8')
        worksheet1 = workbook.add_sheet('Sheet1')
        worksheet2 = workbook.add_sheet('Sheet2')
        for i in range(len(self.df)):
            for j in range(len(self.df[0])):
                worksheet1.write(i, j, self.df[i][j])
        sheet2_colname = ['渠道', '广告ID', '更改为', '出价模式', '状态更改', '计划名称', '计划内广告平均消耗', '计划ROI', '现在出价', '现在消耗', '现在ROI']
        for i in range(len(sheet2_colname)):
            worksheet2.write(0, i, sheet2_colname[i])
        self.planId_col = 3 + self.planId_col
        self.cost_col = 3 + self.cost_col
        self.roi_col = 3 + self.roi_col
        change_num = 1
        style1 = xlwt.XFStyle()
        font1 = xlwt.Font()  # 创建一个文本格式,包括字体、字号和颜色样式特性
        font1.colour_index = 3  # 设置其字体颜色
        font1.bold = True
        style1.font = font1  # 将赋值好的模式参数导入Style
        style2 = xlwt.XFStyle()
        font2 = xlwt.Font()  # 创建一个文本格式,包括字体、字号和颜色样式特性
        font2.colour_index = 2  # 设置其字体颜色
        font2.bold = True
        style2.font = font2  # 将赋值好的模式参数导入Style
        for i in range(1, len(self.df)):
            if self.df[i][self.planId_col] in self.planId_cnt[0]:
                plan_index = self.planId_cnt[0].index(self.df[i][self.planId_col])

                worksheet2.write(change_num, 0, self.df[i][0])
                worksheet2.write(change_num, 1, int(self.df[i][1]))
                if self.df[i][2] < 0.5:
                    worksheet2.write(change_num, 2, self.df[i][2] + 0.2, style1)
                else:
                    worksheet2.write(change_num, 2, round(self.df[i][2] * percent, 1), style1)
                worksheet2.write(change_num, 3, self.df[i][3])
                worksheet2.write(change_num, 5, self.df[i][7])
                worksheet2.write(change_num, 6, self.planId_cnt[3][plan_index])
                worksheet2.write(change_num, 7, self.planId_cnt[2][plan_index])
                worksheet2.write(change_num, 8, self.df[i][2])
                worksheet2.write(change_num, 9, self.df[i][self.cost_col])
                worksheet2.write(change_num, 10, self.df[i][self.roi_col])
                change_num += 1

        workbook.save('广告报表.xls')

        print('下载完成!调整后建议在sheet2当中,如需调整请直接在sheet2里调整。')
        time.sleep(1.5)


class adjustPrice():
    def __init__(self, session):
        a = int(input('调整方案确认无误后,可以开始调价请输入 1 :'))
        if a == 1:
            self.headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
            self.session1 = session
            self.result_list = [[], []]
            self.getExcel()
            self.changeStatus()
            self.changePrice()
            num = len(self.result_list[0])
            if num > 0:
                workbook = xlwt.Workbook(encoding='utf-8')
                worksheet = workbook.add_sheet('Sheet1')
                worksheet.write(0, 0, '广告Id')
                worksheet.write(0, 1, '错误详情')
                for i in range(num):
                    worksheet.write(i + 1, 0, self.result_list[0][i])
                    worksheet.write(i + 1, 1, self.result_list[1][i])
                    workbook.save('修改失败广告ID.xls')
                print('部分广告修改失败,错误信息已保存至  修改失败广告ID.xls')
            else:
                print('全部修改成功!')
        else:
            print('程序结束!')



    def getExcel(self):
        price_file = xlrd.open_workbook('广告报表.xls').sheets()[1]
        media_list = price_file.col_values(0, start_rowx=1, end_rowx=None)
        self.media = media_list
        tem_dict = {
            '腾讯':['gdt/ad/modadstate', 1],
            '百度':['bd/ad/modadstate', 4],
            '头条':['jrtt/ad/modadstate_v2', 2],
            '京x':['adx/ad/modadstate', 5]}
        self.media_list = []
        for i in media_list:
            self.media_list.append(tem_dict[i])
        id_list = price_file.col_values(1, start_rowx=1, end_rowx=None)
        self.id_list = [int(i) for i in id_list]
        price_list = price_file.col_values(2, start_rowx=1, end_rowx=None)
        self.price_list = [int(i * 100) for i in price_list]
        adbid_list = price_file.col_values(3, start_rowx=1, end_rowx=None)
        self.adbid_list = [int(i) for i in adbid_list]

        self.status_list = price_file.col_values(4, start_rowx=1, end_rowx=None)


    def changeStatus(self):
        n = len(self.id_list)
        for num in range(n):
            if self.status_list[num] == '暂停':
                while True:
                    url = 'https://zt.jd.com/fcgi-bin/%s?g_tk=946974209&g_ty=ajax&adIdList=%d&adState=2&mediaType=%d' % (self.media_list[num][0], self.id_list[num], self.media_list[num][1])
                    time.sleep(1)
                    req = self.session1.get(url, headers=self.headers)
                    resultText = json.loads(req.text)['msg']
                    if '频繁' not in resultText:
                        print(self.id_list[num], resultText)
                        if resultText != '操作成功':
                            self.result_list[0].append(self.id_list[num])
                            self.result_list[1].append(resultText)
                        break
            elif self.status_list[num] == '开启':
                while True:
                    url = 'https://zt.jd.com/fcgi-bin/%s?g_tk=946974209&g_ty=ajax&adIdList=%d&adState=1&mediaType=%d' % (self.media_list[num][0], self.id_list[num], self.media_list[num][1])
                    time.sleep(2)
                    req = self.session1.get(url, headers=self.headers)
                    resultText = json.loads(req.text)['msg']
                    if '频繁' not in resultText:
                        print(self.id_list[num], resultText)
                        if resultText != '操作成功':
                            self.result_list[0].append(self.id_list[num])
                            self.result_list[1].append(resultText)
                        break
            else:
                pass


    def changePrice(self):
        tem_dict = {
            '腾讯': 'gdt',
            '头条': 'jrtt',
            '百度': 'bd',
            '京x': 'adx'
        }
        n = len(self.id_list)
        for i in range(n):
            if self.status_list[i]  != '暂停' :
                if self.adbid_list[i] == 16:
                    url = 'https://zt.jd.com/fcgi-bin/%s/ad/modbidprice?g_tk=16342571&g_ty=ajax&bidprice=0.0&adid=%d&adbidtype=16&tcpaprice=%d&datatype=json' % (tem_dict[self.media[i]], self.id_list[i], self.price_list[i])
                elif self.adbid_list[i] == 1:
                    url = 'https://zt.jd.com/fcgi-bin/%s/ad/modbidprice?g_tk=16342571&g_ty=ajax&bidprice=%d&adid=%d&adbidtype=1&datatype=json' % (tem_dict[self.media[i]], self.price_list[i], self.id_list[i])
                elif self.adbid_list[i] == 2:
                    url = 'https://zt.jd.com/fcgi-bin/%s/ad/modbidprice?g_tk=16342571&g_ty=ajax&bidprice=%d&adid=%d&adbidtype=2&datatype=json' % (tem_dict[self.media[i]], self.price_list[i], self.id_list[i])
                while True:
                    time.sleep(1)
                    req = self.session1.get(url, headers=self.headers)
                    resultText = json.loads(req.text)['msg']
                    if '频繁' not in resultText:
                        print(self.id_list[i], resultText)
                        if resultText != '操作成功':
                            self.result_list[0].append(self.id_list[i])
                            self.result_list[1].append(resultText)
                        break
                    print(self.id_list[i], resultText)
            else:
                pass


def main():
    downTask = adReport()
    if downTask.result == 'ok':
        changePrice = adjustPrice(downTask.session)
    else:
        print('程序结束。')
        a = input('')


if __name__ == '__main__':
    main()
# # 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值