首页 > 解决方案 > 美丽的汤,get_text 但不是文本.. 我怎样才能得到它?

问题描述

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

我需要在一列中获取数字182 ,在另一列中获取 58。我已经有了 span,但是当我调用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]:https ://i.stack.imgur.com/ld9qo.png

标签: pythonpython-3.xweb-scrapingbeautifulsoupmysql-python

解决方案


你的问题的答案与这个问题的答案几乎相同。

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]

推荐阅读