python - 美丽的汤,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
解决方案
你的问题的答案与这个问题的答案几乎相同。
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]
推荐阅读
- java - 将字节保存到 pdf 文件并压缩
- java - 什么是 value:while{} 在 Java 中的含义?
- javascript - 使用 Javascript 在列中呈现动态计数 div
- c - 使用单个 HANDLE 和两次调用 ReadProcessMemory 时句柄无效
- python - 如何从 Python 中的对象中删除停用词
- google-chrome - 使用 HTTP/1.1 时浏览器如何决定何时打开新连接?
- c - 在 C 中使用特定数字打印图案
- python - Perl 和 Python 在预声明函数方面的区别
- mongodb - 无法使用 mongoose populate() 填充文档
- batch-file - 在批处理脚本中从 FOR 循环中分配变量中的值