-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipeScraper.py
72 lines (56 loc) · 1.82 KB
/
recipeScraper.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
# Author: Chad Clough
# github/cclough715
#
# Created: 3/28/2015
import urllib3
import bs4
from bs4 import BeautifulSoup
import time
from datetime import datetime
import pickle
http = urllib3.PoolManager()
class Recipe:
def __init__(self, name, author):
self.name = name
self.author = author
self.attributes = []
self.ingredients = []
def __str__(self):
obj = str(self.name) + " by: " + str(self.author) + "\nattributes: "
for attribute in self.attributes:
obj = obj + str(attribute) + ", "
obj = obj + "\ningredients: "
for ingredient in self.ingredients:
obj = obj + str(ingredient) + ", "
return obj
def add_ingredient(self, ingredient):
self.ingredients.append(ingredient)
def add_attribute(self, attribute):
self.attributes.append(attribute)
def get_soup_data(url):
''' Gets the contents of a url in the form of a BeautifulSoup object
Args:
url: the url we're trying to scrape
Returns:
BeautifulSoup; a bs4 object with the contents of url
'''
try:
response = http.request('GET', url)
except urllib3.exceptions.HTTPError, e:
print('HTTPError = ' + str(e))
return
except Exception, e:
print("Error = " + str(e))
return
return bs4.BeautifulSoup(response.data)
def save_object(obj, path):
''' Serialzes an object and saves it into a file
Args:
obj: The object to be serialized
path: The filepath of where we want to save the object
'''
with open(path, 'wb') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def get_object(path):
file = open(path, 'rb')
return pickle.load(file)