Home > Software design >  Scrapping only one element of the webpage under same class
Scrapping only one element of the webpage under same class

Time:04-17

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  style="--margin-top:9px;"><span >4 guests</span><span aria-hidden="true"> · </span><span >2 bedrooms</span><span aria-hidden="true"> · </span><span >2 beds</span><span aria-hidden="true"> · </span><span >1 bath</span></div>

CodePudding user response:

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)

CodePudding user response:

Thanks a lot. I guess the bedroom element is better.

  • Related