0
votes

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

2
could you try string instead of text and check it again. And also add output of your code to your question.iliya
@iliya the output is: K75,000 $750000/m2Esperanza
did you remove strip() from from string? Do it like this and report back.iliya
I updated the code : listing_price = house.find("div", class_="listing-price").string but this time I do not get a value back. I have "None" as the resultEsperanza
could you give me the url so I have the chance to explore it for myselfiliya

2 Answers

2
votes

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
0
votes

You might want to try this:

soup.find('div', {'class': 'listing-price'}).get_text(strip=True, separator='|').split('|')[0]