def post(self): args = parser.parse_args() if not args.get("name"): abort(400, message="name is required") if (args.get("price") or 0) <= 0: abort(400, message="price needs to be higher than 0") p = Product(name=args["name"], price=args["price"]) img = args.get("image") if img: if "." in img.filename: ext = args["image"].filename.split(".")[1] else: ext = "jpg" fname = f"{uuid.uuid4()}.{ext}" img.save(os.path.join("media", fname)) p.image = fname p.save() return _to_json(p), 201
def add_item(): if item_category not in buttons.category_simple_list: item_category_buttons = input('Как назовем список с названием категории?(внутренне название) Напишите название: ') print ('OK! Тебе нужно будет самому создать в файле buttons.py list с этим именем, все оствльное создам автоматом!') buttons.category_simple_list.extend(item_category) buttons.category[item_category] = item_category_buttons try: buttons.item_category_buttons.append(InlineKeyboardButton(item_name, callback_data = item_name) except: print('ну я же говорил, создай List!') with open('auchan_replace.csv', 'a', encoding='utf-8') as f: fields =['name_old', 'name_new'] writer = csv.DictWriter(f, fields, delimiter =';') for row in writer: writer.writerow(item_name_auchan,item_name) with open('metro_replace.csv', 'a', encoding='utf-8') as f: fields =['name_old', 'name_new'] writer = csv.DictWriter(f, fields, delimiter =';') for row in writer: writer.writerow(item_name_metro,item_name) with open('perekrestok_replace.csv', 'a', encoding='utf-8') as f: fields =['name_old', 'name_new'] writer = csv.DictWriter(f, fields, delimiter =';') for row in writer: writer.writerow(item_name_perekrestok,item_name) auchan_parcer.url_list.append(item_url_auchan) metro_parser.url_list.append(item_url_metro) perekrestok_parser.url_list.append(item_url_perekrestok) prod = Product(name = item_name) db_session.add(prod) db_session.commit() print ('OK! ') if item_name not in buttons.item_list: buttons.item_list.extend(item_name) add_item() else: print('Товар уже существует')
def process_item(self, item, spider): """ for x in item['images']: print x['path'] print '-' * 60 """ try: county = session.query(Area).filter(Area.name.like("%s%%" % item["county"])).first() city = session.query(Area).filter(Area.id == county.pid).first() province = session.query(Area).filter_by(id=city.pid).first() province.id except: log.msg(u"未找到省市区县 %s" % item["url"], level=log.WARNING) return item p = Product() p.title = item["title"] p.body = item["body"] p.created = item["created"] p.phone = item["phone"] p.contact = item["contact"] p.address = item["address"] p.city = county.pid p.province = province.id p.county = county.id p.category = item["category"] p.price = item["price"] session.add(p) try: session.commit() except: session.rollback() log.msg(item, level=log.ERROR) return item for img in item["images"]: imgp = Myfile() imgp.path = img["path"] # print img['path'] imgp.product_id = p.id session.add(imgp) try: session.commit() except: log.msg(u"图片保存不成功 %s" % item["url"], level=log.WARNING) session.rollback() return item
#wait_event = WebDriverWait(driver, 10) spec_values = wait_event.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@class="specListItem col-sm-9 col-xs-8 pad10 "]'))) spec_values = [v.text for v in spec_values] for stog_cnt in range(len(storages)): for spc_cnt in range(len(specs_names)): item = Product( store = 'verizon', product_name = product_name, skuId = skuId, brand = brand, color = colors, storage = storages[stog_cnt], price = prices[stog_cnt], spec_name = specs_names[spc_cnt], spec_value = spec_values[spc_cnt], ) item.save() # Click review button to go to the review section review_button = driver.find_element_by_id('reviewsLink') review_button.click() #overall review if num_reviews >0: overall_elm = overall_elm = driver.find_element_by_xpath('.//div[@class=" row noSideMargin deviceReviews border_bottomBlack padTop10 padBottom24"]') overall_text = re.findall(r'[\d.]+', overall_elm.find_element_by_xpath('.//div[@class="col-xs-6 noSidePad pad5"]').text)
def get(self): return [_to_json(p) for p in Product.select()]
def _get_or_404(p_id): try: return Product.get_by_id(p_id) except peewee.DoesNotExist: abort(404, message="Product {} doesn't exist".format(p_id))