python nested Tags (beautiful Soup)
·
Answer a question
I used beautiful soup using python to get data from a specific website but I don't know how to get one of these prices but I want the price in gram (g) AS shown below this is the HTML codeL:
<div class="promoPrice margBottom7">16,000
L.L./200g<br/><span class="kiloPrice">79,999
L.L./Kg</span></div>
I use this code:
p_price = product.findAll("div{"class":"promoPricemargBottom7"})[0].text
my result was: 16,000 L.L./200g 79,999 L.L./Kg
but i want to have: 16,000 L.L./200g only
Answers
You will need to first decompose the span inside the div element:
from bs4 import BeautifulSoup
h = """
<div class="promoPrice margBottom7">16,000 L.L./200g<br/>
<span class="kiloPrice">79,999 L.L./Kg</span></div>
"""
soup = BeautifulSoup(h, "html.parser")
element = soup.find("div", {'class': 'promoPrice'})
element.span.decompose()
print(element.text)
#16,000 L.L./200g
更多推荐

所有评论(0)