I need to get just the top div tag without its nested tags.
<div class="listing-price">
K75,000
<span class="listing-price-sqm">$750000/m<sup>2</sup></span>
</div>
The code I have returns both the value from the div class tag and the span class tag: listing_price = house.find("div", class_="listing-price").text.strip()
How can I get just the value K75,000?
Thanks
You can use contents
or next_element
to get the value.
html='''<div class="listing-price">
K75,000
<span class="listing-price-sqm">$750000/m<sup>2</sup></span>
</div>'''
soup=BeautifulSoup(html,"html.parser")
print(soup.select_one(".listing-price").contents[0].strip())
OR
print(soup.select_one(".listing-price").next_element.strip())
OutPut:
K75,000
You might want to try this:
soup.find('div', {'class': 'listing-price'}).get_text(strip=True, separator='|').split('|')[0]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.