Разбор второй/третьей страницы и добавление в список с помощью BeautifulSoup
Я пытаюсь найти на сайте рецепты и затем представить страницу со случайным выбором. Для этого я создал часть кода, который отлично работает, когда я получаю только первую страницу, 35 рецептов. Однако: я хочу получить рецепты со 2-й и 3-й страницы. Я подумал, что для этого нужно написать цикл, но не могу понять, как это сделать. Что я сделал не так в этом коде?
from django.shortcuts import render
import requests
import re
from bs4 import BeautifulSoup
import random
# Create your views here.
def recipe(request):
#Create soup
page = 0
while page != 2:
webpage_response = requests.get("https://www.ah.nl/allerhande/recepten-zoeken?page=" + str(page))
webpage = webpage_response.content
soup = BeautifulSoup(webpage, "html.parser")
recipe_links = soup.find_all('a', attrs={'class' : re.compile('^display-card_root__.*')})
recipe_pictures = soup.find_all('img', attrs={'class' : re.compile('^card-image-set_imageSet__.*')})
recipe_prep_time = [ul.find('li').text
for ul in soup.find_all('ul',
attrs={'class': re.compile('^recipe-card-properties_root')})]
#Set up lists
links = []
titles = []
pictures = []
#create prefix for link
prefix = "https://ah.nl"
#scrape page for recipe
for link in recipe_links:
links.append(prefix + link.get('href'))
for title in recipe_links:
titles.append(title.get('aria-label'))
for img in recipe_pictures:
pictures.append(img.get('data-srcset'))
page = page +1
#create random int to select a recipe
nummer = random.randint(0,105)
print(nummer)
#select correct link for image
pic_url = pictures[nummer].split(' ')
#create context
context = {
"titles" : titles[nummer],
"pictures" : pic_url[16],
"preptime" : recipe_prep_time[nummer],
"link" : links[nummer]
}
#render page
return render(request, "randomRecipe/recipe.html", context)
Вы можете сделать пагинацию с помощью for loop
from django.shortcuts import render
import requests
import re
from bs4 import BeautifulSoup
import random
# Create your views here.
def recipe(request):
#Create soup
for page in range(1,5):
webpage_response = requests.get(f"https://www.ah.nl/allerhande/recepten-zoeken?page={page}" )
webpage = webpage_response.content
soup = BeautifulSoup(webpage, "html.parser")
recipe_links = soup.find_all('a', attrs={'class' : re.compile('^display-card_root__.*')})
recipe_pictures = soup.find_all('img', attrs={'class' : re.compile('^card-image-set_imageSet__.*')})
recipe_prep_time = [ul.find('li').text
for ul in soup.find_all('ul',
attrs={'class': re.compile('^recipe-card-properties_root')})]
#Set up lists
links = []
titles = []
pictures = []
#create prefix for link
prefix = "https://ah.nl"
#scrape page for recipe
for link in recipe_links:
links.append(prefix + link.get('href'))
for title in recipe_links:
titles.append(title.get('aria-label'))
for img in recipe_pictures:
pictures.append(img.get('data-srcset'))
#create random int to select a recipe
nummer = random.randint(0,105)
print(nummer)
#select correct link for image
pic_url = pictures[nummer].split(' ')
#create context
context = {
"titles" : titles[nummer],
"pictures" : pic_url[16],
"preptime" : recipe_prep_time[nummer],
"link" : links[nummer]
}
#render page
return render(request, "randomRecipe/recipe.html", context)