def get_products(cat, subcat, url): print('Downloading from %s' % url) rez = requests.get(url, verify=False) soup = BeautifulSoup(rez.text, 'html.parser') for item in soup.findAll('div', {'class': 'company_pic'}): img = item.find('img') in_stop = False # отсеиваем ненужное for w in STOP_WORDS: if img.get('title').find(w) > -1: in_stop = True if img.get('src').find('no_image') > -1: in_stop = True if not in_stop: print(img.get('title')) pr = Product() pr.category = cat pr.name = img.get('title') pr.subcategory = subcat img_url = 'https://gastronoma.net/%s' % img.get('src') img_response = requests.get(img_url, stream=True, verify=False) # сохраняем файл with open('tmp.png', 'wb') as out_file: shutil.copyfileobj(img_response.raw, out_file) # читаем временный файл и загружаем его программно в модель with open('%s/tmp.png' % BASE_DIR, 'rb') as img_file: pr.image.save('cat.png', File(img_file), save=True) pr.save()
def post(self, request, format=None): cat = Category.objects.get(pk=request.data.get('cat')) subcat = SubCategory.objects.get(pk=request.data.get('subcat')) p = Product() p.category = cat p.user = request.user.userprofile p.subcategory = subcat p.name = request.data.get('name') if "image" in request.data: p.image = (request.data['image']) if "image_base64" in request.data: try: format, imgstr = request.data.get('image_base64').split( ';base64,') ext = format.split('/')[-1] data = ContentFile(base64.b64decode(imgstr)) file_name = '%s_user.%s' % (p.id, ext) p.image.save(file_name, data, save=True) except: pass p.save() return Response(ProductSerializer(p).data)
def get_products(cat, subcat, url): try: shutil.rmtree('%s/media/category/subcategory' % BASE_DIR) # очищает католог от картинок except: pass print('Downloading fm %s' % url) requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) rez = requests.get(url, verify=False) soup = BeautifulSoup(rez.text, 'html.parser') for item in soup.findAll('div', {'class': 'company_pic'}): img = item.find('img') in_stop = False # отсеиваем ненужное for w in STOP_WORDS: if img.get('title').find(w) > -1: in_stop = True if img.get('src').find('no_image') > -1: in_stop = True if not in_stop: print(img.get('title')) pr = Product() pr.category = cat pr.name = img.get('title') pr.subcategory = subcat img_url = 'https://gastronoma.net/%s' % img.get('src') requests.packages.urllib3.disable_warnings( category=InsecureRequestWarning) img_response = requests.get(img_url, stream=True, verify=False) # сохраняем временный файл with open( 'tmp.png', 'wb' ) as out_file: # создается временный файл 'b' - binary, для записи 'w'-write shutil.copyfileobj(img_response.raw, out_file) # читаем временный файл и загружаем его программно в модель with open('%s/tmp.png' % BASE_DIR, 'rb') as img_file: pr.image.save('product.png', File(img_file), save=True) pr.save()