From fc5dd132ce9d147cc9ca1d8cff144ef8bbd5fa20 Mon Sep 17 00:00:00 2001 From: Ching Date: Fri, 8 Mar 2024 16:28:37 +0800 Subject: [PATCH] fix(recipe): Refactor get_recipe_from_xiachufang to handle steps without images --- .vscode/settings.json | 3 ++- recipe.py | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8774407..3fa8b60 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "conventionalCommits.scopes": [ - "api" + "api", + "recipe" ] } diff --git a/recipe.py b/recipe.py index 68ce597..8405d12 100644 --- a/recipe.py +++ b/recipe.py @@ -15,6 +15,11 @@ def get_recipe_from_xiachufang(url): steps = [] for step in soup.find('div', class_='steps').find('ol').find_all('li'): - steps.append([step.find('p').text.strip(), step.find('img')['src']]) + step_text = step.find('p').text.strip() + step_img = step.find('img') + if step_img: + steps.append([step_text, step_img['src']]) + else: + steps.append([step_text]) return {'name': name, 'ingredients': ingredients, 'steps': steps}