Добавили обработку исключения шагов приготовления, где нету фото
This commit is contained in:
27
function.py
27
function.py
@@ -2,6 +2,9 @@ import requests
|
||||
from bs4 import BeautifulSoup as bs
|
||||
import re
|
||||
|
||||
from dns.name import empty
|
||||
|
||||
|
||||
def try_request(link, max_retries=5):
|
||||
retries = 0
|
||||
while retries < max_retries:
|
||||
@@ -93,24 +96,35 @@ def extr_ingredient(main_container):
|
||||
|
||||
return tags
|
||||
|
||||
|
||||
|
||||
def extr_steps(main_container):
|
||||
|
||||
main_container = main_container.find_all(class_='stepphotos')
|
||||
# На сайте есть страницы исключения по шагам готовки. Фото есть не везде, тогда ищем класс detailed_step_description_big noPhotoStep
|
||||
# Класс detailed_step_description_big noPhotoStep ищет текст через get_text(), а не через тег title
|
||||
|
||||
steps = []
|
||||
count = 1
|
||||
|
||||
for items in main_container[1:]:
|
||||
recipeInstructions = main_container.find(class_='instructions')
|
||||
|
||||
main_container = recipeInstructions.find_all(class_='stepphotos')
|
||||
|
||||
# Проверяем страницу исключение
|
||||
if not main_container:
|
||||
main_container = recipeInstructions.find_all(class_='detailed_step_description_big noPhotoStep')
|
||||
|
||||
|
||||
for items in main_container:
|
||||
img = items.get('href')
|
||||
title = items.get('title')
|
||||
|
||||
# Если класс detailed_step_description_big noPhotoStep, то ищем через get_text. Сейчас title пустой, тк его нет на странице
|
||||
if title is None:
|
||||
title = items.get_text() #Теперь тайтл заполнен
|
||||
print(title)
|
||||
|
||||
steps.append({
|
||||
'img': img,
|
||||
'title': title
|
||||
})
|
||||
|
||||
return steps
|
||||
|
||||
|
||||
@@ -135,6 +149,5 @@ def extr_steps(main_container):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user