'Scrapping only one element of the webpage under same class

I want to scrap only "2 bedrooms" from the below HTML.

in Python I write below command

listings2 = soup.find("div", {"class":"i1wgresd dir dir-ltr"}).get_text()
print(listings2)
<div class="i1wgresd dir dir-ltr" style="--margin-top:9px;"><span class="mp2hv9t dir dir-ltr">4 guests</span><span aria-hidden="true"> · </span><span class="mp2hv9t dir dir-ltr">2 bedrooms</span><span aria-hidden="true"> · </span><span class="mp2hv9t dir dir-ltr">2 beds</span><span aria-hidden="true"> · </span><span class="mp2hv9t dir dir-ltr">1 bath</span></div>


Solution 1:[1]

You can do:

listings2 = soup.find_all("span", {"class": "mp2hv9t"})[1].text
print(listings2)

Prints:

2 bedrooms

But I'd recommend to select only element that contains "bedroom":

bedrooms = soup.select_one('span:-soup-contains("bedroom")').text
print(bedrooms)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrej Kesely