def parse(self, response): for item in response.xpath('//body//div[@class="mw-content-ltr"]//li/a/@href'): yield response.follow('http://dota.wikia.com' + str(item.extract()), self.parse) # yield {'heroe': 'http://dota.wikia.com' + str(hero.extract())} item_name = response.url.split('/')[-1] item_desc = response.xpath('//body//table[@class="infobox"]//td[@colspan="2"]//i/text()').extract()[0] item_effect = response.xpath('//body//table[@class="infobox"]//tr[4]//td[last()]/text()').extract()[0] item_cost = response.xpath('//body//table[@class="infobox"]//tr[6]//td[last()]/text()').extract()[0] item = Item() item.name = item_name item.description = re.sub('<.*?>', '', item_desc).replace('\n', '') item.effects = re.sub('<.*?>', '', item_effect).replace('\n', '') item.cost = int(re.sub('<.*?>', '', item_cost).replace('\n', '')) mongo_item = { 'name': item_name, 'description': re.sub('<.*?>', '', item_desc).replace('\n', ''), 'effects': re.sub('<.*?>', '', item_effect).replace('\n', ''), 'cost': int(re.sub('<.*?>', '', item_cost).replace('\n', '')), } items.insert_one(mongo_item) session.add(item) session.commit() self.log(item_effect) session.close()
def populate_sample_items(): num_items = 30 lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum".split( " ") user = User.objects.get(username="******") for i in range(num_items): item = Item() item.owner = user item.name = " ".join(random.sample(lorem, rand_int(3, 10))) item.description = " ".join(random.sample(lorem, rand_int(10, 20))) item.quantity = rand_int(0, 7) item.cost = float(rand_int(0, 10)) item.save() print "{} sample entries added!".format(num_items)
def save(cls, request: Request, *args, **kwargs): """ Метод для сохранения товара :param request: :param kwargs: :return: """ if request.get("id", False): item = catalog.get_item(int(request.get("id"))) else: item = Item() item.title = request.get("title") item.article = request.get("article") item.short = request.get("short") item.imgs = request.get("imgs", []) item.body = request.get("body", "") item.tags = request.get("tags", []) item.categories = request.get("categories", []) item.cost = int(request.get("cost", 0)) if str(request.get("cost")).isnumeric() else None item.discount = int(request.get("discount")) if str(request.get("discount")).isnumeric() else None item.quantity = int(request.get("quantity")) if str(request.get("quantity")).isnumeric() else None item.set_attributes(request.get("attributes", [])) return {"item_id": item.save()}