G
G
Gorin432020-11-05 17:33:48
Python
Gorin43, 2020-11-05 17:33:48

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))


I will be grateful)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-11-05
@Gorin43

Your return tovari is on the wrong level in your code.
In the code like this:
5fa40f23847dc433994007.png
but it should be like this:
5fa40f312ab8b418903304.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question