python嵌套标记(漂亮的汤)

python嵌套标记(漂亮的汤),python,beautifulsoup,Python,Beautifulsoup,我使用了BeautifulSoup,使用python从特定网站获取数据 但我不知道如何得到这些价格中的一个,但我想要以克(g)为单位的价格 如下所示,这是HTML代码: <div class="promoPrice margBottom7">16,000 L.L./200g<br/><span class="kiloPrice">79,999 L.L./Kg</span></div> 16000 L.L./200g79999 L.

我使用了BeautifulSoup,使用python从特定网站获取数据 但我不知道如何得到这些价格中的一个,但我想要以克(g)为单位的价格 如下所示,这是HTML代码:

<div class="promoPrice margBottom7">16,000 
L.L./200g<br/><span class="kiloPrice">79,999 
L.L./Kg</span></div>
16000
L.L./200g
79999 L.L./Kg
我使用以下代码:

p_price=product.findAll(“div{“class”:“promoPricemargBottom7”})[0]。text

我的结果是:
16000升/千克79999升/千克

但我想: 16000升/200克 仅

您需要首先在
div
元素中输入span:

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
从bs4导入美化组
h=”“”
16000升/200克
79999升/千克 """ soup=BeautifulSoup(h,“html.parser”) element=soup.find(“div”,{'class':'promoPrice'}) element.span.decompose() 打印(element.text) #16000升/200克
您首先需要在
div
元素中输入span:

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
从bs4导入美化组
h=”“”
16000升/200克
79999升/千克 """ soup=BeautifulSoup(h,“html.parser”) element=soup.find(“div”,{'class':'promoPrice'}) element.span.decompose() 打印(element.text) #16000升/200克
尝试使用
汤。选择一个('div.promoPrice')。内容[0]

尝试使用
汤。选择一个('div.promoPrice')。内容[0]


你可以在得到所有这些后单独过滤掉g个。是的,但如何过滤掉列表中的g个。用“g\t”替换“g”。现在,在“\t”上拆分。现在,检查字符串是否有“Kg”。如果是,则忽略,如果不是,则得到你想要的。你可以在得到所有这些后单独过滤掉g个。是的,但如何过滤掉列表中的g个?对吗?替换用“g\t”替换“g”。现在,在“\t”上拆分。现在,检查字符串是否有“Kg”。如果是,则忽略,如果否,则得到所需的内容。
 16,000 L.L./200g