-
Notifications
You must be signed in to change notification settings - Fork 0
/
allrecipes.py
165 lines (140 loc) · 5.44 KB
/
allrecipes.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Authors: Chad Clough, Dan Smith
# github/cclough715
#
# Created: 3/28/2015
import urllib3
import bs4
from bs4 import BeautifulSoup
import recipeScraper
import csv
def export_csv(recipes, filename):
#attribute indexes
nutrition_info = 0
calories = 0
carbs = 1
cholesterol = 2
fat = 3
fiber = 4
protein = 5
sodium = 6
name = ''
file = open(filename, 'wb')
try:
writer = csv.writer(file)
#create header
writer.writerow(('Name', 'Calories', 'Carbohydrates',
'Cholesterol-Free', 'Fat',
'Fiber', 'Protein', 'Sodium'))
for recipe in recipes:
#collect nutritional information
cal_perc = strip_percent(recipe.attributes[nutrition_info][calories])
fat_perc = strip_percent(recipe.attributes[nutrition_info][fat])
car_perc = strip_percent(recipe.attributes[nutrition_info][carbs])
fib_perc = strip_percent(recipe.attributes[nutrition_info][fiber])
pro_perc = strip_percent(recipe.attributes[nutrition_info][protein])
sod_perc = strip_percent(recipe.attributes[nutrition_info][sodium])
try:
row = (
getattr(recipe, 'name'), cal_perc, car_perc,
recipe.attributes[nutrition_info][cholesterol]['amount'] == '0',
fat_perc, fib_perc, pro_perc, sod_perc
)
except:
continue #skip over invalid data
writer.writerow(row)
except:
print name
finally:
file.close()
def strip_percent(attribute):
if '<' in attribute:
return '0'
else:
return float(attribute['percent'].strip('%'))
def import_csv(filename):
#TODO: implement
return recipe
def get_recipe(url):
''' Gets a recipe from allrecipes
Args:
url: The url to the recipe that were scraping
Returns:
A recipe object with the recipe scraped from the url
'''
soup = recipeScraper.get_soup_data(url)
#scrape recipe name and author
try:
name = soup.findAll('h1', {"id" : "itemTitle"})[0].text
except IndexError:
print ("\tError: Recipe name not found")
name = 'N/A'
try:
author = soup.find('span', {"class" : "author"}).text
except IndexError:
print ("\tError: Author not found")
author = 'N/A'
#create the recipe
dish = recipeScraper.Recipe(encode(name), encode(author))
#scrape nutritional information
nutrition = soup.findAll('ul', {'id' : 'ulNutrient'})
nutrition_info = []
for nutrient in nutrition:
nutrient_info = {
'nutrient' : encode(nutrient.find('li', {'class' : 'categories'}).text),
'amount' : encode(nutrient.find('span', {'id' : 'lblNutrientValue'}).text),
'percent' : encode(nutrient.find('li', {'class' : 'percentages'}).text)
}
nutrition_info.append(nutrient_info)
dish.add_attribute(nutrition_info)
if len(nutrition_info) == 0:
print "\tError: No nutritional information is available for reci" \
"pe: {} by {}".format(name, author)
#gather ingredient list for recipe
ingredients = soup.findAll('span', {"class" : "ingredient-name"})
for ingredient in ingredients:
dish.add_ingredient(encode(ingredient.text))
if len(ingredients) == 0:
print "\tError: No ingredients found. Something is wrong here..." \
"\nrecipe: {} by {}".format(name, author)
return dish
def get_recipes(category):
''' Gets a list of all recipes found in a specific category
Args:
category: A search term for a type of recipe (ex. 'italian',
'asian', etc.)
Returns:
A list of all recipes found within the category
'''
recipes = list()
page = 1
url = 'http://www.allrecipes.com/Recipes/' + category
soup = recipeScraper.get_soup_data(url)
has_recipes = True #check if there are any recipes on the page
only_collections_left = False #check if there is only collections left
while has_recipes:
print ("Scraping page: %d..." % (page))
#grab each recipe on this search page
recipe_links = soup.findAll('a', {"class" : "title"})
if len(recipe_links) > 0:
only_collections_left = True
#scrape each recipe on this page
for link in recipe_links:
if link is not None:
#ensure we don't count staff picks twice
if('StaffPicks' not in link['id'] and
'browsedeeper' not in link['href']): #not a collection
r_url = 'http://www.allrecipes.com' + link.get('href')
recipes.append(get_recipe(r_url))
only_collections_left = False
#get next page
page = page + 1
url = 'http://allrecipes.com/Recipes/' + category
url += '/main.aspx?Page=' + str(page)
soup = recipeScraper.get_soup_data(url)
else:
has_recipes = False
if only_collections_left:
has_recipes = False
return recipes
def encode(text):
return text.encode('ascii', 'ignore')