mongo 数据库存储
Posted sakura3
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongo 数据库存储相关的知识,希望对你有一定的参考价值。
mongo 数据库,获取有赞的数据.
from app import mongo from app.external.yz.goods_api import YzGoodsApi from openpyxl import Workbook class YzGoods: # 初始化 def __init__(self): self.yz_api_goods = YzGoodsApi() self.collection = mongo.db.yz_goods_bx self.collection_tag = mongo.db.yz_tags_bx # 抓取商品信息存入mongo数据库 def storage_mongo_goods(self): gooods_itemid_list = self.get_goodslist() for itemid in gooods_itemid_list: goods = self.collection.find_one("_id": itemid) if goods: continue else: goods = self.yz_api_goods.get_goods(item_id=itemid) goods[‘_id‘] = itemid self.collection.insert_one(goods) # 抓取商品类目存入mongo数据库 def storage_mongo_tags(self): self.collection_tag.remove() page_size = 10 page_no = 1 while True: get_tagid = self.yz_api_goods.get_Taglist(page_no=page_no, page_size=page_size) for item in get_tagid: self.collection_tag.insert_one(item) if len(get_tagid) < page_size: break else: page_no += 1 def getgoods(self, item_id): goods = self.collection.find_one("_id": item_id) if goods: return goods else: goods = self.yz_api_goods.get_goods(item_id=item_id) goods[‘_id‘] = item_id self.collection.insert_one(goods) return goods # 获取商品id def get_goodslist(self): value_list = [] page_size = 100 page_no = 1 while True: goods_list = self.yz_api_goods.get_goods_list(page_no=page_no, page_size=page_size, show_sold_out=2) keys = ‘item_id‘ for out_dict in goods_list: tmp = out_dict[keys] value_list.append(tmp) if len(goods_list) < page_size: break else: print(page_no) page_no += 1 return value_list # 获取商品详细信息 def get_goods_id(self): goods_list = self.get_goodslist() value_list = [] for item_id in goods_list: id = item_id goods = self.getgoods(item_id=id) keys = ( ‘item_id‘, ‘title‘, ‘item_no‘, ‘created‘, ‘cid‘, ‘tag_ids‘, ‘quantity‘, ‘sold_num‘, ‘price‘ ) # 好好理解这一块,字典,列表,字典里的值.循环取出的情况 tmp = for keys in keys: tmp[keys] = goods[keys] if ‘skus‘ in goods and goods[‘skus‘]: for sku in goods[‘skus‘]: tmp[‘item_id‘] = str(goods[‘item_id‘]) + ‘-‘ + str(sku[‘sku_id‘]) value_list.append(tmp.copy()) else: tmp[‘item_id‘] = str(goods[‘item_id‘]) value_list.append(tmp.copy()) return value_list def make_goods_excel(self): out_list = self.get_goods_id() # 创建excel工作簿 wb = Workbook() # 第一个excel_sheet1 ws = wb[wb.sheetnames[0]] x = 2 y = 1 # 外层循环字典的key ,里层循环字典的VALUER. 明白了. # 修改。思考一下,一个循环。 for out_dict in out_list: dict_keys = out_dict.keys() out_dict["tags"] = out_dict[‘tag_ids‘] if ‘tag_ids‘ in dict_keys else out_dict[ ‘tag_id‘] if ‘tag_id‘ in dict_keys else "" if ‘tag_ids‘ in dict_keys: del out_dict[‘tag_ids‘] if "tag_id" in dict_keys: del out_dict[‘tag_id‘] for entry in out_list: for key, item in entry.items(): value = item if key == ‘tags‘: value = "/".join([str(t) for t in item]) ws.cell(row=x, column=y, value=value) y = y + 1 x += 1 y = 1 wb.save(R‘C:\Users\HBX\Desktop\工作\201904\业务激励分配\商品表3.xlsx‘) def make_goods_id_excel(self): out_list = self.get_tagid() # 创建excel工作簿 wb = Workbook() # 第一个excel_sheet1 ws = wb[wb.sheetnames[0]] x = 2 y = 1 for out_dict in out_list: for item in out_dict: values = out_dict[item] ws.cell(row=x, column=y, value=values) y = y + 1 x += 1 y = 1 wb.save(R‘C:\Users\HBX\Desktop\工作\201904\业务激励分配\商品表4.xlsx‘) if __name__ == ‘__main__‘: YzGoods().storage_mongo_tags() # YzGoods().make_goods_id_excel() # a = 1
以上是关于mongo 数据库存储的主要内容,如果未能解决你的问题,请参考以下文章