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

Add description method to WikiCookbook scraper #604

Merged
merged 1 commit into from Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions recipe_scrapers/wikicookbook.py
Expand Up @@ -38,3 +38,25 @@ def instructions(self):
return "\n".join(
[normalize_string(instruction.get_text()) for instruction in instructions]
)

def description(self):
paragraphs = list()
for tag in self.soup.find(class_="mw-parser-output"):
TheEpic-dev marked this conversation as resolved.
Show resolved Hide resolved
try:
# get all paragraphs except for links
if (
tag.text.strip()
and tag.name == "p"
and not tag.find("span", {"id": "displaytitle"})
TheEpic-dev marked this conversation as resolved.
Show resolved Hide resolved
):
paragraphs.append(tag)
# End at the TOC or second section
if tag.attrs.get("id") == "toc" or tag.name == "h2":
break
except AttributeError:
# Ignore tags that are not <p> but raise errors
pass

return "\n\n".join(
[normalize_string(paragraph.get_text()) for paragraph in paragraphs]
)
6 changes: 6 additions & 0 deletions tests/test_wikibooks.py
Expand Up @@ -53,3 +53,9 @@ def test_instructions(self):
"Preheat oven to 350 °F (180 °C).\nBlend all ingredients, except the pie shell, together.\nPour into the unbaked pie shell.\nBake at 350 °F (180 °C) for 45 minutes.\nLet cool and serve.",
self.harvester_class.instructions(),
)

def test_description(self):
return self.assertEqual(
"Pumpkin pie is a traditional American and Canadian holiday dessert. It consists of a pumpkin-based custard baked in a single pie shell. The pie is traditionally served with whipped cream.",
self.harvester_class.description(),
)