<div class="Ticket_last_24 report_table_right">
<span>13586</span>
<span>(</span>
<span class="change_decrease">-6.5%</span>
<span>)</span>
</div>
<div class="Ticket_last_24 report_table_right">
<span>99492</span>
<span>(</span>
<span class="change_decrease">+1.4%</span>
<span>)</span>
</div>
以上是我的HTML。我只想要13586、99492。
这是我正在使用的代码。
TicketNumber= []
tickets_by_Lanuages = soup.select('div[class="tickets_by_language"]')[0]
ALLnum= tickets_by_Lanuages.findAll('span')
for p in ALLnum:
TicketNumber.append(p.text)
It is printing out like it:
[
['13586',
'(',
'-6.4%',
')',
'99492',
'(',
'+1.4%',
')']
我如何使用美丽的汤只选择数字['13568','99492']
答案 0 :(得分:0)
您可以使用CSS选择器:
data = """<div class="Report_last_week report_table_right">
<span> 11232</span>
<span>(</span>
<span class="change_decrease">-6.2%</span>
</span>)</span>
</div>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'lxml')
print(soup.select_one('div span').text.strip())
打印:
11232
如果您想更具体一点,可以指定带有<div>
标签的类。以下代码具有相同的效果:
print(soup.select_one('div.Report_last_week.report_table_right span').text.strip())