In [6]:
from pattern.web import URL, DOM, plaintext, Element, extension, DEPTH
import re

In [2]:
test_urls = ["http://allrecipes.com/recipe/19125","http://allrecipes.com/recipe/8854/"]

In [11]:
def get_ingredients(element):
    ing_nodes = element.by_class("recipe-ingred_txt added")
    return "\n".join([plaintext(a.content) for a in ing_nodes])

In [7]:
def get_instructions(element):
    instr_nodes = element.by_class("recipe-directions__list--item")
    return "\n".join([plaintext(a.content) for a in instr_nodes])

In [21]:
def save_image(element,idx):
    first_rec_photo = element.by_class("rec-photo")[0]
    url = first_rec_photo.attributes.get('src','')
    print("Image URL", url)
    img_url = URL(url)
    f = open("img/"+str(idx) + extension(img_url.page), 'wb')
    f.write(img_url.download())
    f.close()

In [22]:
data = []
for idx,test_url in enumerate(test_urls):
    url = URL(test_url)
    html = url.download(cached=True)
    element = Element(html)
    rec_id =element.by_tag(str)
    ing_str = get_ingredients(element)
    instruction_str = get_instructions(element)
    data.append(ing_str+"\n"+instruction_str)
    save_image(element, idx)


('Image URL', u'http://images.media-allrecipes.com/userphotos/250x250/565645.jpg')
('Image URL', u'http://images.media-allrecipes.com/userphotos/720x405/2204020.jpg')

In [23]:
for rec in data:
    print rec


1 tablespoon olive oil
1 tablespoon minced garlic
3 pounds skinless, boneless chicken breast halves
1 tablespoon paprika
1 cup brown sugar
1 cup red wine
salt and pepper to taste
Add all ingredients to list
Add all ingredients to list
Heat oil in a large skillet over medium high heat. Cook garlic in oil until tender. Place chicken in the skillet, and cook about 10 minutes on each side, until no longer pink and juices run clear.
Drain oil from skillet. Sprinkle chicken with paprika and 1 cup brown sugar. Pour red wine around chicken. Cover, and simmer about 15 to 20 minutes; lightly baste chicken with wine sauce while cooking. Season to taste with salt and pepper.

1 pound chopped fresh broccoli
1 1/2 cups cubed, cooked chicken meat
1 (10.75 ounce) can condensed cream of broccoli soup
1/3 cup milk
1/2 cup shredded Cheddar cheese
1 tablespoon butter, melted
2 tablespoons dried bread crumbs
Add all ingredients to list
Add all ingredients to list
Preheat oven to 450 degrees F (230 degrees C).
Place the broccoli in a saucepan with enough water to cover. Bring to a boil, and cook 5 minutes, or until tender. Drain.
Place the cooked broccoli in a 9 inch pie plate. Top with the chicken. In a bowl, mix the soup and milk, and pour over the chicken. Sprinkle with Cheddar cheese. Mix the melted butter with the bread crumbs, and sprinkle over the cheese.
Bake in the preheated oven for 15 minutes, or until bubbly and lightly brown.


In [ ]: