Answer the question
In order to leave comments, you need to log in
Why does the parser output only the first category?
For some reason, the parser instead of a list of categories gives out only the first one.
import requests
from bs4 import BeautifulSoup
URL = 'http://www.zagrya.ru/'
HEADERS = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
}
def get_html(url, params = ''):
r = requests.get(url,headers =HEADERS, params=params)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('li', class_='hor-menu__item has-subm')
tovari = []
for item in items:
tovari.append(
{
'karegoria': item.find('a', class_='hor-menu__lnk').find('span', class_='hor-menu__text').get_text(),
}
)
return tovari
html = get_html(URL)
print(get_content(html.text))
Answer the question
In order to leave comments, you need to log in
Your return tovari is on the wrong level in your code.
In the code like this:
but it should be like this:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question