W
W
wolron2020-05-13 16:08:27
Python
wolron, 2020-05-13 16:08:27

How to extract data by key from class variable 'bs4.element.resultset'?

after parsing the page

soup = BeautifulSoup(html.content, 'html.parser')
items = soup.find_all('form', {"id":"command"})

the items variable has the 'bs4.element.resultset' class, it contains everything that was in the form tag with the command id.

There is data:

[{"title" : "холодильник", "price" : "100 руб" , "цвет" : "белый"} ,
{"title" : "стиральная машина", "price" : "200 руб" , "цвет" : "черный"},
{"title" : "миксер", "price" : "50 руб" , "цвет" : "красный"}]

It looks like a list of dictionaries, but if you check the len(items) methods, the result is 1. That is, there is only one element in the items variable.

I need to make a selection by the title key. But title is not a tag, but text. How can I do this so that all the data after the title is entered into the dictionary and into the list?

A list of this type should be formed:

холодильник
стиральная машина
миксер

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Ilyin, 2020-05-14
@sunsexsurf

It looks like a list of dictionaries, but if you check the len(items) methods, the result is 1. That is, there is only one element in the items variable.

you first have everything in the list (list, []), in which there is already a dict.
Try items[0] - the first element will be your dictionary.
This happened because the find_all method returns a list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question