首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python嵌套标签(美丽的汤)

python嵌套标签(美丽的汤)
EN

Stack Overflow用户
提问于 2020-05-13 01:54:46
回答 2查看 63关注 0票数 1

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

代码语言:javascript
复制
<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克

EN

回答 2

Stack Overflow用户

发布于 2020-05-13 03:42:40

您需要首先在div元素内对跨度执行decompose操作:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-05-13 03:55:03

尝试使用soup.select_one('div.promoPrice').contents[0]

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61758669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档