欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)比對美團接口返回數(shù)據(jù)和本地mongo數(shù)據(jù)是否一致示例

 更新時間:2019年08月09日 09:24:38   作者:zhizunyu2009  
這篇文章主要介紹了python實現(xiàn)比對美團接口返回數(shù)據(jù)和本地mongo數(shù)據(jù)是否一致,涉及Python基于requests模塊的數(shù)據(jù)請求與比較運算相關(guān)操作技巧,需要的朋友可以參考下

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

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

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)前的時間戳
  t = time.time()
  return int(t)
def req_get_result(api_url,api_data):#get方法請求函數(shù)
  req_get = requests.get(api_url,api_data)
  result = req_get.json()
  return result
def req_post_result(api_url,api_data):#post方法請求函數(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]#去掉最后一個非門店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)) +'個商品'
    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+"售價不一致,美團:"+skus['price']+',數(shù)據(jù)庫:'+str(shopPrice)
        file_edit(file_name,wirte_price)
      if float(skus['stock']) != stock:
        wirte_stock = sku_id+"庫存不一致,美團:"+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)不一致,美團:"+is_sold+',數(shù)據(jù)庫:'+is_sold_out
        file_edit(file_name,wirte_sold)
      print('已完成',sku_id)
  client.close()

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

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

相關(guān)文章

  • python tkinter中的錨點(anchor)問題及處理

    python tkinter中的錨點(anchor)問題及處理

    這篇文章主要介紹了python tkinter中的錨點(anchor)問題及處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • python 公共方法匯總解析

    python 公共方法匯總解析

    這篇文章主要介紹了python 公共方法匯總解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • python利用pymysql和openpyxl實現(xiàn)操作MySQL數(shù)據(jù)庫并插入數(shù)據(jù)

    python利用pymysql和openpyxl實現(xiàn)操作MySQL數(shù)據(jù)庫并插入數(shù)據(jù)

    這篇文章主要為大家詳細介紹了如何使用Python連接MySQL數(shù)據(jù)庫,并從Excel文件中讀取數(shù)據(jù),將其插入到MySQL數(shù)據(jù)庫中,有需要的小伙伴可以參考一下
    2023-10-10
  • Python時間序列處理之ARIMA模型的使用講解

    Python時間序列處理之ARIMA模型的使用講解

    今天小編就為大家分享一篇關(guān)于Python時間序列處理之ARIMA模型的使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • 用python寫爬蟲簡單嗎

    用python寫爬蟲簡單嗎

    在本篇內(nèi)容里小編給大家整理的是關(guān)于用python寫爬蟲是否簡單的相關(guān)內(nèi)容文章,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • 利用Pygame繪制圓環(huán)的示例代碼

    利用Pygame繪制圓環(huán)的示例代碼

    這篇文章主要介紹了利用Python中的Pygame模塊繪制一個彩色的圓環(huán),文中的示例代碼講解詳細,對我們學(xué)習(xí)Pygame有一定幫助,需要的可以參考一下
    2022-01-01
  • python3中dict(字典)的使用方法示例

    python3中dict(字典)的使用方法示例

    這篇文章主要介紹了python3中dict(字典)的使用方法,文中給出了詳細的功能列舉,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • 詳解在OpenCV中如何使用圖像像素

    詳解在OpenCV中如何使用圖像像素

    像素是計算機視覺中圖像的重要屬性。它們是表示圖像中特定空間中光的顏色強度的數(shù)值,是圖像中數(shù)據(jù)的最小單位。本文將詳細為大家介紹如何在OpenCV中使用圖像像素,感興趣的可以了解一下
    2022-03-03
  • Python如何同時讀寫Excel

    Python如何同時讀寫Excel

    這篇文章主要介紹了Python如何同時讀寫Excel問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python中pd.cut()與pd.qcut()的對比及示例

    python中pd.cut()與pd.qcut()的對比及示例

    本文主要介紹了python中pd.cut()與pd.qcut()的對比及示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06

最新評論