gpt4 book ai didi

python - BeautifulSoup ,get_text 但不是 文本..我怎样才能得到它?

转载 作者:行者123 更新时间:2023-12-02 02:35:48 33 4
gpt4 key购买 nike

鉴于此标记:[标记][1]

我需要在一列中获取数字182,在另一列中获取数字58。我已经有了跨度,但是当我调用div.get_tex()或字符串时,它返回=18258(两个数字)

这是我的代码_:

prices= soup.find_all('div', class_='grilla-producto-precio')

cents= []
price= []
for px in prices:
### here i need to get the number 182 and append it to "price"
for spn in px.find('span'):
cents.append(spn)

如果没有跨度,我如何单独获得价格 182?谢谢!!!![1]:/image/ld9qo.png

最佳答案

您的问题的答案与 this question 的答案几乎相同.

from bs4 import BeautifulSoup

html = """
<div class = "grilla-producto-precio">
" $"
"182"
<span>58</span>
</div>
"""
soup = BeautifulSoup(html,'html5lib')

prices = soup.find_all('div',class_ = "grilla-producto-precio")

cents = []

for px in prices:
txt = px.find_next(text=True).strip()

txt = txt.replace('"','')

txt = int(txt.split("\n")[-1])

cents.append(txt)

输出:

[182]

关于python - BeautifulSoup ,get_text 但不是 <span> 文本..我怎样才能得到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64337413/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com