-
Notifications
You must be signed in to change notification settings - Fork 1
/
retriveRecipes.py
66 lines (53 loc) · 1.65 KB
/
retriveRecipes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import requests
from bs4 import BeautifulSoup
def getRecipe(name_recipe):
# URL GENERATED BY RECIPE NAME FOR GET REQUEST TO allrecipes.com
name_recipe_parsed = name_recipe.split(" ")
url = "https://www.allrecipes.com/search/results/?wt="
i = 0
len_ = len(name_recipe_parsed)
for s in name_recipe_parsed:
if i != len_ - 1:
url+=s+"%20"
else:
url += s
i += 1
url += "&sort=re"
r = requests.get(url)
# BUILD STRING FOR COMPARE
string_comp = ""
i = 0
for s in name_recipe_parsed:
if i != len_-1:
string_comp += s.lower().replace("'","")+"-"
else:
string_comp += s.lower()
i += 1
# PARSER FOR FIND RECIPE PAGE
soup = BeautifulSoup(r.text, 'html.parser')
cards = soup.find_all(class_="fixed-recipe-card__h3")
for c in cards:
url_recipe = c.find("a").get('href')
if str(url_recipe).find(string_comp) != -1:
break
r2 = requests.get(url_recipe)
# PARSER FOR EXTRACT STEPS OF RECIPES
soup = BeautifulSoup(r2.text, "html.parser")
box = soup.find(class_="directions--section__steps")
box_split = box.text.strip("\n").replace("\n"," ").split(" ")
steps = []
prep_time = "4"
i=0
for b in box_split:
if b:
steps.append(b)
i+=1
if steps[2]:
prep_time = steps[2].replace(" Ready In","").replace(" m","")
else:
prep_time = "6"
steps = steps[3:]
if len(prep_time) > 2:
prep_time = "4"
calories = 0
return steps, prep_time, calories