饿了么(分时双转数据).py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2022/10/8 10:58
  3. # @Author : Clown
  4. # @File : 饿了么(分时双转数据)zzx.py
  5. # @Software : PyCharm
  6. import requests
  7. import csv
  8. import os
  9. import sys
  10. from datetime import datetime,timedelta
  11. import json
  12. import pymysql
  13. import random
  14. import time
  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. host = '124.222.188.59'
  77. passwd = '111...Clown'
  78. db_name = 'zuzu_data'
  79. port = 63306
  80. db = linkTomySql (host, passwd, db_name, port)
  81. brand_name = '浆小白'
  82. owner = 'all'
  83. keys_dict = {'elm': '',
  84. 'elm_shops': '',
  85. 'mt': '',
  86. 'mt_shops': ''}
  87. keys_dict['elm'] = read_key_value_pair (db, brand_name, 'ELEME', owner)
  88. keys_dict['elm_shops'] = get_shops_info_to_list (db, brand_name, 'ELEME', '')
  89. keys_dict['mt'] = read_key_value_pair (db, brand_name, 'MEITUAN', owner)
  90. keys_dict['mt_shops'] = get_shops_info_to_list (db, brand_name, 'MEITUAN', '')
  91. try:
  92. ksid = keys_dict['elm']['data']['session']['ksid']
  93. elm_shops = keys_dict['elm_shops']
  94. script_name = os.path.basename (sys.argv[0])
  95. beginTime = (datetime.today () + timedelta (days=-1)).strftime ('%Y-%m-%d') # 2022-06-23
  96. endTime = (datetime.today () + timedelta (days=0)).strftime ('%Y-%m-%d') # 2022-06-23
  97. beginTime_int = int ((datetime.today () + timedelta (days=-1)).strftime ('%Y%m%d')) # 20220623
  98. endTime_int = int ((datetime.today () + timedelta (days=0)).strftime ('%Y%m%d')) # 20220623
  99. if 1==1:
  100. save_path = 'F:/cppc/cppc_数据表/3每日爬虫数据/饿了么分时双转数据/'
  101. title = ["门店id", "时段", "曝光人数", "进店人数", "进店转化率%", "下单人数", "下单次数", "下单转化率%", '分时收入',"店名", "数据日期"]
  102. csv_name_last = str(beginTime_int) + '饿了么分时双转数据(浆小白).csv'
  103. f_last = open (save_path+csv_name_last, mode='w', newline='', encoding="utf-8-sig")
  104. writer_last = csv.writer (f_last) # 这一步是创建一个csv的写入
  105. writer_last.writerow (title)
  106. # csv_name_today = str (endTime_int) + '饿了么分时双转数据(即时).csv'
  107. # f_today = open (save_path + '即时数据/' + csv_name_today, mode='w', newline='', encoding="utf-8-sig")
  108. # writer_today = csv.writer (f_today) # 这一步是创建一个csv的写入
  109. # writer_today.writerow (title)
  110. url = 'https://lsycm.alibaba.com/api/naposOverview/queryOverviewRealTimeHourData'
  111. for shop in elm_shops:
  112. time.sleep (random.uniform (0.5, 1.5))
  113. shop_id = shop['shop_id']
  114. shop_name = shop['shop_name']
  115. params = {"shopId":int(shop_id),
  116. "endDate":str(endTime_int),
  117. "compareOption":"YESTERDAY"}
  118. headers = {
  119. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36',
  120. 'content-type': 'application/json',
  121. 'token': f'{ksid}={shop_id}'}
  122. resp = requests.post(url,json=params,headers=headers).json()
  123. try:
  124. data = resp['data']
  125. for h in range(0,24):
  126. last_day_exp_pv = data['expUvDist']['compareValue'][str (h)] #曝光人数
  127. last_day_clk_pv = data['clkUvDist']['compareValue'][str (h)] #进店人数
  128. last_day_clk_exp_rate = data['clkExpRateDist']['compareValue'][str (h)] #进店转化率
  129. last_day_valid_order_user_cnt = data['validOrderUserCntDist']['compareValue'][str (h)] #下单人数
  130. last_day_valid_order_cnt = data['validOrderDist']['compareValue'][str (h)] #下单次数
  131. last_day_ord_clk_rate = data['ordClkRateDist']['compareValue'][str (h)] #下单转化率
  132. incomeDist = data['incomeDist']['compareValue'][str (h)] #收入
  133. last_day_row = [shop_id, h, last_day_exp_pv, last_day_clk_pv, last_day_clk_exp_rate, last_day_valid_order_user_cnt, last_day_valid_order_cnt, last_day_ord_clk_rate, incomeDist, shop_name, beginTime]
  134. writer_last.writerow (last_day_row)
  135. print (script_name, shop_id, shop_name, 'inputing')
  136. except:
  137. # print (resp)
  138. last_day_row = [shop_id, -1, -1, -1, -1, -1, -1, -1, -1, shop_name, beginTime]
  139. writer_last.writerow (last_day_row)
  140. print (script_name, shop_id, shop_name, 'error')
  141. except:
  142. print(script_name,'有误')