Repository: hhursev/recipe-scrapers
Pull request #295 (2021-01-09 06:01:14)

TasteOfHome: Seperates instructions by newline characters
The schema data for Taste of Home separates the instructions by a comma rather than a new line character and also includes HTML characters on occasion.

Switching to parse the instructions from the HTML to separate them by new line characters and avoid including HTML in the instructions themselves. Will fall back to the data from the schema in case the parsing fails to find anything.


Patch:
diff --git a/recipe_scrapers/tasteofhome.py b/recipe_scrapers/tasteofhome.py
index 723304d1..97a4c4f4 100644
--- a/recipe_scrapers/tasteofhome.py
+++ b/recipe_scrapers/tasteofhome.py
@@ -1,4 +1,5 @@
 from ._abstract import AbstractScraper
+from ._utils import normalize_string
 
 
 class TasteOfHome(AbstractScraper):
@@ -22,7 +23,17 @@ class TasteOfHome(AbstractScraper):
         return self.schema.ingredients()
 
     def instructions(self):
-        return self.schema.instructions()
+        instructions = self.soup.findAll("li", {"class": "recipe-directions__item"})
+        if instructions:
+            return "\n".join(
+                [
+                    normalize_string(instruction.get_text())
+                    for instruction in instructions
+                ]
+            )
+        else:
+            # In case our HTML parsing doesn't find any instructions, fall back to what the schema provides.
+            return self.schema.instructions()
 
     def ratings(self):
         return self.schema.ratings()


Test patch:
diff --git a/tests/test_tasteofhome.py b/tests/test_tasteofhome.py
index 5e52213e..244ffdd6 100644
--- a/tests/test_tasteofhome.py
+++ b/tests/test_tasteofhome.py
@@ -58,7 +58,7 @@ class TestTasteOfHomeScraper(ScraperTest):
 
     def test_instructions(self):
         return self.assertEqual(
-            "Place cloves, peppercorns and bay leaf on a double thickness of cheesecloth; bring up corners of cloth and tie with kitchen string to form a bag. In a large bowl, combine the water, vinegar, sugar, salt and ginger. Add beef and spice bag; let stand at room temperature for 30 minutes.,    Transfer all to a 6-qt. electric pressure cooker. Add carrots, celery and onion. Lock the lid and close pressure-release valve. Adjust to pressure-cook on high for 10 minutes. Quick-release pressure. Select saute setting and adjust for medium heat; bring liquid to a boil. Discard the spice bag. Stir in gingersnaps; cook and stir until thickened, about 3 minutes. Serve with egg noodles. If desired, top with parsley and pepper.,   <br><b>Freeze option: </b> Freeze cooled sauerbraten in freezer containers. To use, partially thaw in refrigerator overnight. Heat through in a saucepan, stirring occasionally; add a little broth or water if necessary.",
+            "Place cloves, peppercorns and bay leaf on a double thickness of cheesecloth; bring up corners of cloth and tie with kitchen string to form a bag. In a large bowl, combine the water, vinegar, sugar, salt and ginger. Add beef and spice bag; let stand at room temperature for 30 minutes.\nTransfer all to a 6-qt. electric pressure cooker. Add carrots, celery and onion. Lock the lid and close pressure-release valve. Adjust to pressure-cook on high for 10 minutes. Quick-release pressure. Select saute setting and adjust for medium heat; bring liquid to a boil. Discard the spice bag. Stir in gingersnaps; cook and stir until thickened, about 3 minutes. Serve with egg noodles. If desired, top with parsley and pepper.\nFreeze option: Freeze cooled sauerbraten in freezer containers. To use, partially thaw in refrigerator overnight. Heat through in a saucepan, stirring occasionally; add a little broth or water if necessary.",
             self.harvester_class.instructions(),
         )