Files
Parser_recipes/parser.py

122 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import requests
from bs4 import BeautifulSoup as bs
import re
import function as f
import json
link = 'https://povar.ru/list/'
def pars_group(link):
#Сбор видов блюд
response = f.try_request(link)
soup = bs(response.text, 'html.parser')
main_container = soup.find_all(class_='ingredientItem')
for items in main_container:
item = items.find_all('a')
title = item[0].get_text()
if title == 'Салаты': break
print(title)
for i in item[1::]:
name_group = i.get_text()
link_group = 'https://povar.ru' + i.get('href')
print('-'*5, name_group, link_group)
pars_dishs(title, name_group, link_group)
print('-'*50)
def pars_dishs(title='', name_group='', link='https://povar.ru/list/spagetti/', page=0):
#Сбор списка рецептов
while True:
page += 1
new_link = link + str(page)
soup = f.try_soup(f.try_request(new_link))
if soup == False: break
main_container = soup.find_all(class_='listRecipieTitle')
for items in main_container:
recipe_name = items.get_text()
recipe_link = 'https://povar.ru' + items.get('href')
print(recipe_name, recipe_link)
pars_recipie(title, name_group, recipe_name, recipe_link)
print('-'*50)
def pars_recipie(title=0, name_group=0, recipe_name=0 ,link='https://povar.ru/recipes/slivochnaya_karbonara-73186.html'):
response = f.try_request(link)
soup = bs(response.text, 'html.parser')
main_container = soup.find(class_='cont_area hrecipe')
name_id = link.split('/')[-1]
try:
name_id = name_id.replace('.html', '')
except: pass
print(name_id)
recipies = {'recipes': {}}
detailed_tags = f.extract_tags_from_detailed_tags(main_container) #Собираем теги
print(detailed_tags)
ingredients = f.extr_ingredient(main_container) #Собираем ингредиенты
#print(ingredients)
calories_info = f.extract_nutrition(main_container.find_all(class_='circle')) #БЖУ
#print(calories_info)
steps = f.extr_steps(main_container) #Сборка шагов
#print(steps)
preview_img = steps[-1]['img']
recipies['recipes'] = { '_id' : name_id,
'recipe_name':recipe_name,
'url':link,
'preview_img':preview_img,
'tags':detailed_tags,
'ingredients':ingredients,
'nutritional_value':calories_info,
'steps':steps}
print(recipies)
pars_group(link)
#pars_dishs()
#pars_recipie()