python 利用百度推广API接口,每日调整关键词价格,关键词点击率大于10%的进行降低价格和点击率小于4%进行增加价格

步骤
1、查询昨日关键词报告,获取点击率大于10%和点击率小于4%的关键词信息。
2、调整价格信息,关键词点击率大于10%的关键词价格调整为原价格的95%
关键词点击率小于4%且价格小于10元的,将价格调整为原有价格的1.05倍。

import requests,json,datetime

#获取昨天日期
def getYesterday(): 
    today=datetime.date.today() 
    oneday=datetime.timedelta(days=1) 
    yesterday=today-oneday  
    return yesterday

#获取指定期间关键词信息,返回大于10%的和小于4%的关键词列表,以供后续使用
def select_keywords():

  yesterday = getYesterday()
  url = "https://api.baidu.com/json/sms/service/ReportService/getRealTimeData"

  payload = "{\n\t\"header\":{\n\t\t\"username\":\"【百度推广账户】\",\n\t\t\"token\":\"【百度推广token】\",\n\t\t\"password\":\"【百度推广密码】\"\n\t},\n\t\"body\":{\n\t\t\"realTimeRequestType\":{\n\t\t\t\"performanceData\":[\"impression\",\"click\",\"cost\",\"cpc\",\"ctr\",\"cpm\",\"conversion\"],\n\t\t\t\"levelOfDetails\":11,\n\t\t\t\"startDate\":\""+str(yesterday)+" 00:00:00\",\n\t\t\t\"endDate\":\""+str(yesterday)+" 23:59:59\",\n\t\t\t\"unitOfTime\":1,\n\t\t\t\"reportType\":14\n\t\t}\n\t}\n}"
  headers = {
    'Content-Type': 'text/plain'
  }

  response = requests.request("POST", url, headers=headers, data = payload.encode())
  json_res = json.loads(response.text.encode('utf-8'))
  state = json_res['header']['desc']
  body_data_wordsid = json_res['body']['data'][0]['id']
  #impression\",\"click\",\"cost\",\"cpc\",\"ctr\",\"cpm\",\"conversion\
  than_10_percent_wordid = []
  less_4_percent_wordid = []
  for row in  json_res['body']['data']:
    words_id = row['id']
    words_kpis_impression = row['kpis'][0] #展现量
    words_kpis_click = row['kpis'][1] #点击量
    words_kpis_cost = row['kpis'][2]  #花费
    words_kpis_cpc = row['kpis'][3] #平均点击价格
    words_kpis_ctr = row['kpis'][4] #点击率
    words_kpis_cpm = row['kpis'][5] 
    words_name = row['name'][3] #关键词名称
    #如果点击率大于10%
    if float(words_kpis_ctr) > 0.1:
      #print("大于10%的有",words_name,words_id,words_kpis_impression,words_kpis_click,words_kpis_cost,words_kpis_cpc,'点击率ctr',words_kpis_ctr)
      than_10_percent_wordid.append(words_id)
    elif float(words_kpis_ctr) < 0.04:
      #print("小于4%的有",words_name,words_id,words_kpis_impression,words_kpis_click,words_kpis_cost,words_kpis_cpc,'点击率ctr',words_kpis_ctr)
      less_4_percent_wordid.append(words_id)
  print("ctr大于10%的有",than_10_percent_wordid)
  print("ctr小于4%的有",less_4_percent_wordid)
  return than_10_percent_wordid,less_4_percent_wordid

#通过列表wordid 查询 keyword 信息(价格,名称,id等)
def wordid_select_info(wordiditems):
    url = "https://api.baidu.com/json/sms/service/KeywordService/getWord"
    payload = "{\n\t\"header\":{\n\t\t\"username\":\"【百度推广账户】\",\n\t\t\"token\":\"【百度推广token】\",\n\t\t\"password\":\"【百度推广密码】\"\n\t},\n\t\"body\":{\n\t\t\"ids\":"+str(wordiditems)+",\n\t\t\"idType\":11,\n\t\t\"getTemp\":0,\n\t\t\"wordFields\":[\n\t\t\t\"keywordId\",\n\t\t\t\"keyword\",\n\t\t\t\"adgroupId\",\n\t\t\t\"price\",\n\t\t\t\"matchType\",\n\t\t\t\"pcQuality\",\n\t\t\t\"mobileQuality\"\n\t\t\t]\n\t}\n}"
    headers = {
    'Content-Type': 'text/plain'
    }
    response = requests.request("POST", url, headers=headers, data = payload.encode())
    json_res = json.loads(response.text.encode('utf8'))
    state = json_res['header']['desc']
    words_info = []
    for row in json_res['body']['data']:
        keywordId = row['keywordId']
        price = row['price']
        keyword = row['keyword']
        
        words_info.append([keyword,price,keywordId])
    print(words_info)
    return words_info


#根据wordid更新关键词价格
def update_word_price(wordid,price):
    url = "https://api.baidu.com/json/sms/service/KeywordService/updateWord"

    payload = "{\n\t\"header\":{\n\t\t\"username\":\"【百度推广账户】\",\n\t\t\"token\":\"【百度推广token】\",\n\t\t\"password\":\"【百度推广密码】\"\n\t},\n\t\"body\":{\n\t\t\"keywordTypes\":[\n\t\t\t{\n\t\t\t\t\"keywordId\":"+str(wordid)+",\n\t\t\t\t\"price\":"+str(price)+"\n\t\t\t}\n\t\t\t]\n\t}\n}"
    headers = {
    'Content-Type': 'text/plain'
    }
    response = requests.request("POST", url, headers=headers, data = payload.encode())
    json_res = json.loads(response.text.encode('utf8'))
    state =  json_res['header']['desc']
    print(wordid,"价格成功更新为:",price)
    return state

#点击率大于10%的关键词出价降低5%
def ctr_than_10_percent_reduce_5_percent_price(worditems):
  print()



if __name__ == '__main__':
  print("----开始更新每日价格-----")
  than_10_percent_wordid,less_4_percent_wordid = select_keywords()
  than_10_percent_wordid_words_info = wordid_select_info(than_10_percent_wordid)
  less_4_percent_wordid__words_info = wordid_select_info(less_4_percent_wordid)
  print("ctr大于10%的words")
  #点击率大于10%的,将价格设置为原来的95%
  for word1 in than_10_percent_wordid_words_info:
    word1_id = word1[2]
    word1_price = float(word1[1])
    new_word1_price = word1_price * 0.95
    update_word_price(word1_id,new_word1_price)
    print(word1_id,'--原价格为:',word1_price,"--更新后价格为:",new_word1_price)

  print("ctr小于4%的words")
  #点击率小于4%的价格增加为原来价格的1.05倍。
  for word2 in less_4_percent_wordid__words_info:
    word2_id = word2[2]
    word2_price = float(word2[1]) 
    new_word2_price = word2_price * 1.05
    #仅设置价格小于10的关键词
    if word2_price < 10:
      update_word_price(word2_id,new_word2_price)
      print(word2_id,'--原价格为:',word2_price,"--更新后价格为:",new_word2_price)
    else:
      print(word2_id,"价格大于10的无需更新!现在价格为:",word2_price)
  
  print("---已完成所有价格更新---")
    

执行结果展示如下
python 利用百度推广API接口,每日调整关键词价格,关键词点击率大于10%的进行降低价格和点击率小于4%进行增加价格_第1张图片

你可能感兴趣的:(python学习案例)