def added(request): url1 = str(request.GET['link']) print(url1) headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" } page = requests.get(url1, headers=headers) soup = BeautifulSoup(page.content, 'html.parser') title1 = soup.find('span', {'class': 'a-size-large'}).get_text().strip() price1 = soup.find( 'span', { 'class': 'a-size-medium a-color-price priceBlockBuyingPriceString' }).get_text() image1 = soup.find( 'img', {'id': 'landingImage' })['data-old-hires'] #find method only return single value mrp1 = soup.find('span', { 'class': 'priceBlockStrikePriceString a-text-strike' }).get_text() saving1 = soup.find('td', { 'class': 'a-span12 a-color-price a-size-base priceBlockSavingsString' }).get_text() label_tag = soup.find_all( 'td', class_='label') #find_all return a list of all data with tags included value_tag = soup.find_all( 'td', class_='value') #get_text method return the text inside the html tags ram1 = soup.find('td', {'class': 'value'}).get_text() specs = {} for i, j in zip(label_tag, value_tag): specs[i.get_text().strip().lower()] = j.get_text( ) #there is space around label tag that is why used strip method #used lower method to to lower all character as to avoid confusion which word will be capital and which not print(specs['ram']) product = Product() product.title = title1 product.price = price1 product.image = image1 product.mrp = mrp1 product.saving = saving1 product.url = url1 product.other_images = "do something later" product.android = specs['os'] product.ram = specs['ram'] product.weight = specs['weight'] product.battery = specs['battery power rating'] + " mAh" product.display = specs['display technology'] product.pub_date = timezone.datetime.now() product.save() print('saved') return render(request, 'added.html')
def create(self, request): dic = request.data.dict() id_cat = dic.pop('category', None) cat = Category.objects.filter(pk=id_cat).first() print(cat) pro = Product() pro.category = cat pro.description = dic.pop('description', None) pro.display = dic.pop('display', None) pro.display_in_home = dic.pop('display_in_home', None) pro.name = dic.pop('name', None) pro.offer_discount = dic.pop('offer_discount', None) pro.order = dic.pop('order', None) pro.price = dic.pop('price', None) print(dic) colors = dic.getlist('color_stocks', None) print(colors) return Response('Hola :V')