-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe.py
65 lines (58 loc) · 2.47 KB
/
recipe.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
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import array
import json
import re
import requests
def get_html_format (url):
html_page = uReq(url)
get_html = html_page.read()
html_page.close()
return soup(get_html,"html.parser")
chicken_recipe_html = get_html_format("https://www.allrecipes.com/recipes/201/meat-and-poultry/chicken/")
all_articles = chicken_recipe_html.find(id="main-content").find_all('article')
links = []
for article in all_articles:
if(article.find('a')!=None):
links.append(article.find('a')['href'])
# print (links)
item_list = []
def validate_link(somestring):
return "video" in somestring or "www" not in somestring
for link in links:
item = {}
if(validate_link(link)):
continue
recipe_page = get_html_format(link)
# print(link)
if(recipe_page.find(id='recipe-main-content') != None):
item['name'] = recipe_page.find(id='recipe-main-content').text
if(recipe_page.find(id="polaris-app") != None):
ingredient_box = recipe_page.find(id="polaris-app")
ingredients_list = []
if(ingredient_box.find_all('li') !=None):
for ingredients in ingredient_box.find_all('li'):
if(ingredients.text.strip() != "Add all ingredients to list"):
ingredients_list.append(ingredients.text.strip())
item['ingredients'] = ingredients_list
if(recipe_page.find("div",class_="directions--section")!=None):
directions_list = []
direction_box = recipe_page.find("div",class_="directions--section")
# print(direction_box)
if(direction_box.find_all('ol',class_="recipe-directions__list")!=None):
direction = direction_box.find('ol',class_="recipe-directions__list")
direction = direction.find_all('span',class_='recipe-directions__list--item')
for step in range(0, len(direction)):
directions_list.append(("Step "+ str(step+1) +": "+direction[step].text.replace("Watch Now","")).strip())
item['instructions']= directions_list
item_list.append(item)
for x in item_list:
payload = json.dumps(x)
url = "http://localhost:3000/recipe/add"
headers = {
'Content-Type': "application/json",
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.json())
# r = requests.post('https://recipefinder2018.herokuapp.com/recipe/add',data=json_item)
# print(r.json())