Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed reishunger.de parser #352 #616

Merged
merged 11 commits into from Oct 12, 2022
49 changes: 40 additions & 9 deletions recipe_scrapers/reishunger.py
@@ -1,4 +1,5 @@
# mypy: disallow_untyped_defs=False

from ._abstract import AbstractScraper
from ._utils import normalize_string

Expand Down Expand Up @@ -27,17 +28,47 @@ def ingredients(self):
return self.schema.ingredients()

def instructions(self):
result = self.soup.find("section", {"class": "recipe-preparation"})
if result:
result = "\n".join(
normalize_string(i.get_text()) for i in result.findAll("p")
)
return result
# find the "instructions" heading (Zubereitung in German)
for heading in self.soup.findAll("h3"):
if "Zubereitung" in heading.get_text():
break

results = []

# locate the first recipe instruction
step1 = heading.parent.parent.find("div", {"class": "leading-normal"})

# iterate through each step in the recipe
for step in step1.next_siblings:

# check whether the instruction has a list of preparations
# fixme: this can throw an exception if 'step' is not a bs4 Tag
try:
preparations = step.find("div", {"preparation": True})
except Exception:
preparations = None

# if it does, add every preparation step as an instruction entry
if preparations:
for preparation in preparations.findAll("div", {"id": True}):
instruction = normalize_string(preparation.text)
results.append(instruction)

# otherwise, add only one instruction entry
else:
if step.find("p"):
instruction = normalize_string(step.text)
results.append(instruction)

# continue on to the next instruction
step = step.next_sibling

# filter out empty lines
results = [instruction for instruction in results if instruction]
return "\n".join(results)

def ratings(self):
block = self.soup.find("div", {"id": "recipe-header"}).find(
"div", {"class": "nrating"}
)
block = self.soup.find("div", {"class": "nrating"})
if block:
cnt = len(block.findAll("span", {"class": "fa-star"}))
return cnt
Expand Down