J
J
judywb2020-09-06 22:14:09
Python
judywb, 2020-09-06 22:14:09

How to move to the next element under the same bs4 class?

I scrape a page in VK, got to the location, and in order to parse the name of the city, the following picture is displayed (photo below), and here I need to parse the second labeled, here is my solution

for item in items:
    place = item.find('div', class_='profile_info')
    if place:
        place = place.find('div', class_='clear_fix').nextSibling().find('div', class_='labeled').find('a').get_text()
    else:
        print('не указан')

but it throws an error

AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

How to fix? Tell

5f5534f634387709622862.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-09-06
@judywb

Just check the intermediate results, why try to immediately swallow a huge piece of
print

place = place.find('div', class_='clear_fix').nextSibling()

and you will see that there is a list . Select the desired index from the list, and look for the city in it. In general, why not use the API?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question