如何打印标记中的第一个文本



我有一个汤对象,比如:

<span class="nowrap">
2 633
<span class="currency rub">q</span>
</span>

我做到了:

price = item.find('span', class_='nowrap')
price_x = price.text.strip()
print(price_x)

结果:2 633q.

我怎么能没有"q"。仅:2 633

原因是.text将标记中的所有字符串连接在一起。您应该使用next_element

from bs4 import BeautifulSoup
text = """<span class="nowrap">
2 633
<span class="currency rub">q</span>
</span>"""
soup = BeautifulSoup(text, 'html.parser')
price = soup.find('span', class_='nowrap')
next_element = price.next_element
print(repr(next_element.strip()))

输出:

'2 633'

相关内容

最新更新