-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better nutrition parsing for cookwell.com (#1459)
* add better nutrition parsing for cookwell.com * update tests * linting * Apply suggestions from code review Co-authored-by: Joey <[email protected]> * remove unused variable --------- Co-authored-by: Joey <[email protected]>
- Loading branch information
1 parent
a5937f7
commit f77fbd3
Showing
3 changed files
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
from ._abstract import AbstractScraper | ||
import re | ||
|
||
|
||
class CookWell(AbstractScraper): | ||
@classmethod | ||
def host(cls): | ||
return "cookwell.com" | ||
|
||
def nutrients(self): | ||
nutrition_pattern = re.compile( | ||
r'{\\"calories\\":(?P<calories>\d+),\\"carbohydrates\\":(?P<carbohydrates>\d+),\\"fat\\":(?P<fat>\d+),\\"protein\\":(?P<protein>\d+)}' | ||
) | ||
|
||
nutrition_script = self.soup.find("script", string=nutrition_pattern) | ||
if nutrition_script: | ||
match = nutrition_pattern.search(nutrition_script.string) | ||
if match: | ||
nutrition_info = match.groupdict() | ||
return { | ||
"calories": nutrition_info["calories"], | ||
"carbohydrateContent": f'{nutrition_info["carbohydrates"]} g', | ||
"fatContent": f'{nutrition_info["fat"]} g', | ||
"proteinContent": f'{nutrition_info["protein"]} g', | ||
} | ||
|
||
return self.schema.nutrients() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters