美团(分时竞价数据).py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Author:Clown
  2. # -*- codeing = utf-8 -*-
  3. # @Time :2021/4/14 12:07
  4. # @File: 美团曝光(新).py
  5. from time import sleep
  6. import requests
  7. import csv
  8. import random
  9. import xlrd
  10. import os
  11. import sys
  12. from datetime import datetime,timedelta
  13. import json
  14. import pymysql
  15. def linkTomySql(host, passwd, db_name, port):
  16. '''连接至数据库返回【db】,v2新增local_infile=1 打开文件导入权限'''
  17. try:
  18. # 本地连接为:localhost 服务器连接为:124.222.188.59
  19. db = pymysql.connect (
  20. host=host, user="root",
  21. passwd=passwd,
  22. db=db_name,
  23. charset='utf8mb4',
  24. local_infile=1,
  25. port=port)
  26. # print ('\nconnect to mysql server 成功')
  27. # print ('---------------------------------------')
  28. except:
  29. print ("\ncould not connect to mysql server")
  30. db = "连接失败"
  31. return db
  32. def read_key_value_pair(db, brand_name, wm_plate, owner):
  33. '''按条件读取,数据库中all_key_table表里的key_value_pair字段中的值,以键值对的形式输出
  34. db:数据库,
  35. brand_name:品牌名,
  36. wm_plate:外卖平台MEITUAN或ELEME,
  37. owner:账号权限all或one
  38. '''
  39. cursor = db.cursor ()
  40. sql = f'SELECT key_value_pair FROM all_key_table WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}" AND owner = "{owner}";'
  41. cursor.execute (sql)
  42. pair = json.loads (cursor.fetchall ()[0][0])
  43. return pair
  44. def get_shops_info_to_list(db, brand_name, wm_plate, key_name):
  45. '''获取门店信息表【shops_info_to_list】中的信息,
  46. 并返回表单shops_info_df【shop_id,shop_name,update_datetime,info_for_script】
  47. db:数据库信息
  48. brand_name:品牌
  49. wm_plate:外卖平台
  50. key_name:关键信息字段名,如无填‘’,如有填对应键值对的key
  51. '''
  52. cursor = db.cursor ()
  53. if key_name == '':
  54. sql = f'SELECT shop_id,shop_name,update_datetime FROM shops_info_for_script WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}";'
  55. cursor.execute (sql)
  56. shops_info = cursor.fetchall ()
  57. shops_info_df = []
  58. for shop_info in shops_info:
  59. shop_info_dict = {'shop_id': shop_info[0],
  60. 'shop_name': shop_info[1]}
  61. shops_info_df.append (shop_info_dict)
  62. return shops_info_df
  63. else:
  64. sql = f'SELECT shop_id,shop_name,update_datetime,info_for_script -> "$.{key_name}" FROM shops_info_for_script WHERE brand_name = "{brand_name}" AND wm_plate = "{wm_plate}";'
  65. cursor.execute (sql)
  66. shops_info = cursor.fetchall ()
  67. shops_info_df = []
  68. for shop_info in shops_info:
  69. shop_info_dict = {'shop_id': shop_info[0],
  70. 'shop_name': shop_info[1],
  71. 'update_datetime': shop_info[2],
  72. f'{key_name}': shop_info[3]}
  73. shops_info_df.append (shop_info_dict)
  74. return shops_info_df
  75. if __name__ == '__main__':
  76. # start_time = time.time()
  77. host = '124.222.188.59'
  78. passwd = '111...Clown'
  79. db_name = 'zuzu_data'
  80. port = 63306
  81. db = linkTomySql(host, passwd, db_name, port)
  82. brand_name = '浆小白'
  83. owner = 'all'
  84. keys_dict = {'elm':'',
  85. 'elm_shops':'',
  86. 'mt':'',
  87. 'mt_shops':''}
  88. keys_dict['elm'] = read_key_value_pair(db, brand_name, 'ELEME', owner)
  89. keys_dict['elm_shops'] = get_shops_info_to_list(db,brand_name,'ELEME','')
  90. keys_dict['mt'] = read_key_value_pair (db, brand_name, 'MEITUAN', owner)
  91. keys_dict['mt_shops'] = get_shops_info_to_list (db, brand_name, 'MEITUAN', '')
  92. try:
  93. token = keys_dict['mt']['data']['session']['token']
  94. acctId = keys_dict['mt']['data']['session']['acctId']
  95. mt_shops = keys_dict['mt_shops']
  96. script_name = os.path.basename(sys.argv[0])
  97. beginTime = (datetime.today() + timedelta(days=-1)).strftime('%Y-%m-%d') #2022-06-23
  98. endTime = (datetime.today() + timedelta(days=-1)).strftime('%Y-%m-%d') #2022-06-23
  99. beginTime_int =int((datetime.today() + timedelta(days=-1)).strftime('%Y%m%d')) #20220623
  100. endTime_int = int((datetime.today() + timedelta(days=-1)).strftime('%Y%m%d')) #20220623
  101. title = ["门店id", "时段", "点击次数", "时段竞价花费", "单次点击花费", "曝光次数", "点击率%", "店名", "数据日期"]
  102. with open("F:/cppc/cppc_数据表/3每日爬虫数据/美团分时竞价数据/%s美团分时竞价数据.csv" % str(beginTime_int), 'a', newline='',
  103. encoding="utf-8-sig") as t: # encoding= "utf-8"
  104. writer = csv.writer(t) # 这一步是创建一个csv的写入
  105. writer.writerow(title) # 写入标签
  106. for shop in mt_shops:
  107. shop_id = shop['shop_id']
  108. shop_name = shop['shop_name']
  109. try:
  110. sleep(random.uniform(1, 2))
  111. url = "https://waimaieapp.meituan.com/ad/v3/statistics/cpc/today/info?acctId=%s"%str(acctId)+"&wmPoiId=%s" %str(shop_id)+"&token=%s&platform=0"%str(token)
  112. resp =requests.get(url)
  113. data = resp.json()
  114. data_json = data['data']['yesterday']#昨日数据
  115. for list in data_json:
  116. clickCount = list['clickCount']#点击次数
  117. cost = list['cost']#时段竞价花费
  118. avgPrice = list['avgPrice']#单次点击花费
  119. showCount = list['showCount']#曝光次数
  120. clickRate = list['clickRate']#点击率%
  121. time = list['time']#时段
  122. list_data = [shop_id,time,clickCount,cost,avgPrice,showCount,clickRate,shop_name,beginTime]
  123. writer.writerow(list_data) # 写入样本数据
  124. print(script_name,shop_id,shop_name,'input')
  125. except:
  126. list_data = [shop_id, "error", "error", "error", "error", "error", "error", shop_name,beginTime]
  127. writer.writerow(list_data) # 写入样本数据
  128. print(script_name,shop_id,shop_name,'error')
  129. except Exception as e:
  130. print(e)
  131. '''
  132. workbook = xlrd.open_workbook(r'F:/cppc/cppc_数据表/门店信息表.xlsx')
  133. table = workbook.sheet_by_name('美团')
  134. id_data = table.col_values(3,10)#第4列第11行
  135. row = 9#行号
  136. day = int(table.cell_value(0, 1))#数字日期
  137. day_data = table.cell_value(1, 1)#数据日期
  138. token = table.cell_value(2, 1)#密钥
  139. print(token)
  140. for i in id_data:
  141. row += 1
  142. cell = table.cell_value(row, 4) # 第row行第5列
  143. try:
  144. n = int(i)
  145. sleep(random.uniform(1, 2))
  146. url = "https://waimaieapp.meituan.com/ad/v3/statistics/cpc/today/info?acctId=93908152&wmPoiId=%s" %str(n)+"&token=%s&platform=0"%str(token)
  147. resp =requests.get(url)
  148. data = resp.json()
  149. data_json = data['data']['yesterday']#昨日数据
  150. for list in data_json:
  151. clickCount = list['clickCount']#点击次数
  152. cost = list['cost']#时段竞价花费
  153. avgPrice = list['avgPrice']#单次点击花费
  154. showCount = list['showCount']#曝光次数
  155. clickRate = list['clickRate']#点击率%
  156. time = list['time']#时段
  157. list_data = [[n,time,clickCount,cost,avgPrice,showCount,clickRate,cell,day_data]]
  158. title = ["门店id","时段","点击次数","时段竞价花费","单次点击花费","曝光次数","点击率%", "店名","数据日期"]
  159. with open("F:/cppc/cppc_数据表/3每日爬虫数据/美团分时竞价数据/%s美团分时竞价数据.csv"%str(day), 'a', newline='', encoding="utf-8-sig") as t: # encoding= "utf-8"
  160. writer = csv.writer(t) # 这一步是创建一个csv的写入
  161. writer.writerow(title) # 写入标签
  162. writer.writerows(list_data) # 写入样本数据
  163. print(n, "第%s行"%str(row+1),'inputing')
  164. except:
  165. list_data = [[n, "error", "error", "error", "error", "error", "error", cell,day_data]]
  166. title = ["门店id", "时段", "点击次数", "时段竞价花费", "单次点击花费", "曝光次数", "点击率%", "店名","数据日期"]
  167. with open("F:/cppc/cppc_数据表/3每日爬虫数据/美团分时竞价数据/%s美团分时竞价数据.csv"%str(day), 'a', newline='',
  168. encoding="utf-8-sig") as t: # encoding= "utf-8"
  169. writer = csv.writer(t) # 这一步是创建一个csv的写入
  170. writer.writerow(title) # 写入标签
  171. writer.writerows(list_data) # 写入样本数据
  172. print(n,"第%s行"%str(row+1),'error')
  173. '''