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

标签 python python-3.x web-scraping beautifulsoup mysql-python

鉴于此标记: [标记][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/

相关文章:

python - openpyxl - 在现有合并单元格的 excel 文件中添加新行

python - PySide2-uic 为不相关的小部件生成递增的项目索引

excel - VBA - 网页抓取无法获取 HTMLElement insideText

javascript - 将 HTML 文档的 javascript 部分中的字段提取到表中?地理坐标

python - 在 pandas col 中将每第二行乘以 -1

python - 在 DataFrame 索引中查找标签位置

php - Firebase JWT 库无法验证 Python JWT token

python - PRAW/Tweepy 过滤关键字

python - python 3中的子类文件

python - 使用 Selenium 更改 Google map 评论排序