日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python 美团api接口对接_python实现比对美团接口返回数据和本地mongo数据是否一致示例...

發(fā)布時(shí)間:2025/4/17 115 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 美团api接口对接_python实现比对美团接口返回数据和本地mongo数据是否一致示例... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文實(shí)例講述了python實(shí)現(xiàn)比對(duì)美團(tuán)接口返回?cái)?shù)據(jù)和本地mongo數(shù)據(jù)是否一致。分享給大家供大家參考,具體如下:

應(yīng)用背景:美團(tuán)平臺(tái)商品的上下架狀態(tài)、庫存、售價(jià),和mongo庫存儲(chǔ)的是否一致。

tools文件內(nèi)容

# -*- coding: utf-8 -*-

import hashlib

import time

import requests

def get_md5(string):#返回字符串md5加密后的串

hl = hashlib.md5()

hl.update(string.encode('utf-8'))

return hl.hexdigest()

def get_tamp():#獲取當(dāng)前的時(shí)間戳

t = time.time()

return int(t)

def req_get_result(api_url,api_data):#get方法請(qǐng)求函數(shù)

req_get = requests.get(api_url,api_data)

result = req_get.json()

return result

def req_post_result(api_url,api_data):#post方法請(qǐng)求函數(shù)

req_post = requests.post(api_url,data=api_data)

result = req_post.json()

return result

def file_edit(file_name,wr_str):#寫入txt文件

f1 = open(r'D:\%s.txt'%file_name,'a')

f1.write(wr_str+'\n')

f1.close()

def param_sort(param_dict):#傳入字典,返回排序后并且連接好的字符串

keys_list = sorted(param_dict.keys())

rb_str = ''

for k in keys_list:

key_value = k + '=' + str(param_dict[k])

rb_str = rb_str + key_value +'&'

rb_str = rb_str[0:-1] #不保留字符串末尾的&

return rb_str

下面是主邏輯

# -*- coding: utf-8 -*-

from tools import get_tamp,get_md5,req_get_result,file_edit,param_sort

import conf

import datetime

import time

from pymongo import MongoClient

app_id = conf.appinfo[1]['app_id']

secret = conf.appinfo[1]['secret']

def get_shop_id_list(app_id,secret):#獲取門店id的列表

api_url = 'http://waimaiopen.meituan.com/api/v1/poi/getids'

timestamp = get_tamp()

params_str = api_url+'?app_id=%s&timestamp=%s'%(app_id,timestamp)

sig = get_md5(params_str + secret)

api_data = {

'app_id':app_id,

'sig':sig,

'timestamp':timestamp,

}

result = req_get_result(api_url,api_data)

shop_id_list = result['data']

del shop_id_list[-1]#去掉最后一個(gè)非門店id元素

return shop_id_list

def get_shop_detail(shop_id):#根據(jù)門店id,返回門店名稱

api_url = 'http://waimaiopen.meituan.com/api/v1/poi/mget'

timestamp = get_tamp()

app_poi_codes = shop_id

params_str = api_url+'?app_id=%s&app_poi_codes=%s&timestamp=%s'%(app_id,app_poi_codes,timestamp)

sig = get_md5(params_str + secret)

api_data = {

'app_id':app_id,

'sig':sig,

'timestamp':timestamp,

'app_poi_codes':app_poi_codes

}

result = req_get_result(api_url,api_data)

shop_name = result['data'][0]['name']

return shop_name

def get_goods(shop_id):#根據(jù)門店id,查詢門店商品,返回列表

api_url = 'http://waimaiopen.meituan.com/api/v1/retail/list'

timestamp = get_tamp()

app_poi_code = shop_id

params_str = api_url+'?app_id=%s&app_poi_code=%s&timestamp=%s'%(app_id,app_poi_code,timestamp)

sig = get_md5(params_str + secret)

api_data = {

'app_id':app_id,

'sig':sig,

'timestamp':timestamp,

'app_poi_code':app_poi_code

}

result = req_get_result(api_url,api_data)

return result['data']

if __name__ == '__main__':

shop_ids = get_shop_id_list(app_id,secret)

file_name = datetime.datetime.now().strftime('%Y.%m.%d.%H.%M.%S')

client = MongoClient(conf.mongo_online,conf.mongo_port)

db = client['oh-product']

collection = db.outerShopSku

for shop_id in shop_ids:

shop_name = get_shop_detail(shop_id)

goods_list = get_goods(shop_id)

wirte_shop_info = shop_id + '--' + shop_name + str(len(goods_list)) +'個(gè)商品'

file_edit(file_name,wirte_shop_info)

for i in range(0,len(goods_list)):

skus = eval(goods_list[i]['skus'])[0]

sku_id = skus['sku_id']

result = collection.find({'channel':'MeiTuan','outerShopId':shop_id,'outerSkuId':sku_id})

shopPrice = result[0]['shopPrice'] #int,單位:分

stock = result[0]['stock'] #float

is_sold_out = result[0]['status'] #str online/offline

if round(float(skus['price'])*100) != shopPrice:

wirte_price = sku_id+"售價(jià)不一致,美團(tuán):"+skus['price']+',數(shù)據(jù)庫:'+str(shopPrice)

file_edit(file_name,wirte_price)

if float(skus['stock']) != stock:

wirte_stock = sku_id+"庫存不一致,美團(tuán):"+skus['stock']+',數(shù)據(jù)庫:'+str(stock)

file_edit(file_name,wirte_stock)

if goods_list[i]['is_sold_out'] == 0:

is_sold = 'offline'

else:

is_sold = 'online'

if is_sold != is_sold_out:

wirte_sold = sku_id+":狀態(tài)不一致,美團(tuán):"+is_sold+',數(shù)據(jù)庫:'+is_sold_out

file_edit(file_name,wirte_sold)

print('已完成',sku_id)

client.close()

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

總結(jié)

以上是生活随笔為你收集整理的python 美团api接口对接_python实现比对美团接口返回数据和本地mongo数据是否一致示例...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。