我使用python制作的漂亮汤从特定网站获取数据 但我不知道如何获得这些价格之一,但我想要以克(g)为单位的价格 如下所示,这是HTML代码L:
<div class="promoPrice margBottom7">16,000
L.L./200g<br/><span class="kiloPrice">79,999
L.L./Kg</span></div>
我使用以下代码:
p_price = product.findAll("div{"class":"promoPricemargBottom7"})[0].text
我的结果是:
16,000 L.L./200g 79,999 L.L./Kg
但是我想拥有: 16,000升/ 200克 仅
答案 0 :(得分:0)
您首先需要在div
元素内decompose进行跨度:
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
答案 1 :(得分:0)
尝试使用soup.select_one('div.promoPrice').contents[0]
from bs4 import BeautifulSoup
html = """<div class="promoPrice margBottom7">16,000 L.L./200g<br/>
<span class="kiloPrice">79,999 L.L./Kg</span></div>"""
soup = BeautifulSoup(html, features='html.parser')
# value = soup.select('div.promoPrice > span') # for 79,999 L.L./Kg
value = soup.select_one('div.promoPrice').contents[0]
print(value)
打印
16,000 L.L./200g