forked from hhursev/recipe-scrapers
/
weightwatcherspublic.py
64 lines (50 loc) · 2.21 KB
/
weightwatcherspublic.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
# mypy: allow-untyped-defs
from ._utils import normalize_string
from .weightwatchers import Weightwatchers
# collect the differences between public and non-public weightwatcher recipes in this class
class weightwatchersPublic(Weightwatchers):
@classmethod
def host(cls):
return "www.weightwatchers.com"
def _findDataContainer(self):
return self.soup.find("div", {"class": "HorizontalList_list__GESs0"})
def _extractItemField(self, item):
return item.find("div", {"data-e2e-name": "attribute_item_value"})
def image(self):
return self.soup.find("img", {"class": "FoodMasthead_heroImage__BjVdZ"})["src"]
def nutrients(self):
return self.soup.find("div", {"class": "Coin_text__3UOb0"})["aria-label"]
def description(self):
return normalize_string(
self.soup.find("div", {"data-e2e-name": "food_masthead_detail_description"})
.find("div", {"class": "ReadMoreLess_collapsed__IAzxP"})
.get_text()
)
def instructions(self):
return self._getInstructions(
"h2", "class", "InstructionsFood_headline__vw7cn", "span"
)
def _findIngridientTags(self):
return (
self.soup.find("div", {"class": "IngredientsCard_card__VSY4x"})
.find("div", {"data-e2e-name": "vertical_list_items"})
.find_all("div", recursive=False)
)
def _extractIngridientName(self, ingridient):
return normalize_string(
ingridient.find("p", {"data-e2e-name": "ingredient_name"}).get_text()
)
def _extractPortionParts(self, ingridient):
tags = ingridient.find(
"p", {"data-e2e-name": "ingredient_description"}
).find_all("span")
comment = None
unit = None
if len(tags) > 2:
comment = normalize_string(tags[2].get_text().replace(", ", "", 1))
unit = normalize_string(tags[1].get_text())
else:
descriptionParts = normalize_string(tags[1].get_text()).split(", ", 1)
unit = descriptionParts[0]
comment = descriptionParts[1] if len(descriptionParts) > 1 else None
return (normalize_string(tags[0].get_text()), unit, comment)