V
V
Vadim kyklaed2016-11-04 00:15:28
Python
Vadim kyklaed, 2016-11-04 00:15:28

How to parse if it gives resultSet?

Hello everyone, I ran into a problem that I can’t open the div container,
here is my code for this piece

for row2 in page.html.body.findAll("div","recipe-ing"):
    print(row2.ul.findAll("li","cat"))

the essence of the problem is that I can’t go deeper into the container and no transformations and omission of tags lead to the ReultSet result, how to parse the name and amount values?
[<li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/7777/"><span itemprop="name">Молоко кокосовое</span></a><span itemprop="amount">200 мл</span> </span> </li>, <li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/3562/"><span itemprop="name">Яйцо куриное</span></a><span itemprop="amount">3 шт</span> </span> </li>, <li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/1601/"><span itemprop="name">Сахар</span></a><span itemprop="amount">120 г</span> </span> </li>, <li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/3192/"><span itemprop="name">Ванильный сахар</span></a><span itemprop="amount">20 г</span> </span> </li>, <li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/2278/"><span itemprop="name">Сок лимонный</span></a><span itemprop="amount">1 ч. л.</span> </span> </li>, <li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/351/"><span itemprop="name">Вода</span></a><span itemprop="amount">1 ч. л.</span> </span> </li>, <li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/1068/"><span itemprop="name">Масло сливочное</span></a><span itemprop="amount">1 ч. л.</span> </span> </li>, <li class="cat">
<span itemprop="ingredient" itemscope="" itemtype="http://data-vocabulary.org/RecipeIngredient"><a href="http://www.povarenok.ru/recipes/ingredient/4175/"><span itemprop="name">Конфеты</span></a>	   (Рафаэлло) 	 — <span itemprop="amount">2 шт</span> </span> </li>]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
12ss, 2016-11-04
@kyklaed

I don't understand the popularity of BeautifulSoup.
Take lxml for parsing, learn to write xpath. It's even simpler and more efficient.
There will be something like
In BeautifulSoup try this

spans = soup.find_all('span', attrs={"itemprop": "name"})
for span in spans:
    print(span.get_text())

or two nested loops
lists = soup.find_all("li","cat")
for li in lists:
    spans = li.find_all(attrs={"itemprop": "name"})
    for span in spans:
        print(span.get_text())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question