请在表格中如何获取这些值。我需要日期,时间,保留和播放值。每次我只得到整个表的整个列表时,我不知道如何在其中捕获给定的值,非常感谢您的帮助。
<table class="list">
<tr class="head">
<th>Date</th>
<th>Time</th>
<th>Play</th>
<th>Tickets</th>
<th> </th>
</tr>
<tr class="t1">
<th>Th
03. 09. 2020</th>
<td>
19:00</td>
<td>Racek</td>
<td class="center">4</td>
<td>
<a href="/rezervace/detail?id=2618"
title="Reserve tickets for this performance">
reserve
</a>
</td>
</tr>
该脚本将解析表,BeautifulSoup
然后在屏幕上打印各个行:
import re
from bs4 import BeautifulSoup
html = '''
<table class="list">
<tr class="head">
<th>Date</th>
<th>Time</th>
<th>Play</th>
<th>Tickets</th>
<th> </th>
</tr>
<tr class="t1">
<th>Th
03. 09. 2020</th>
<td>
19:00</td>
<td>Racek</td>
<td class="center">4</td>
<td>
<a href="/rezervace/detail?id=2618"
title="Reserve tickets for this performance">
reserve
</a>
</td>
</tr>
</table>
'''
soup = BeautifulSoup(html, 'html.parser')
all_data = []
for row in soup.select('tr'):
all_data.append([re.sub(r'\s{2,}', ' ', d.get_text(strip=True)) for d in row.select('td, th')])
# print data to screen:
# print header:
print('{:<25}{:<15}{:<15}{:<15}{:<15}'.format(*all_data[0]))
# print rows:
for date, time, play, tickets, reserve in all_data[1:]:
print('{:<25}{:<15}{:<15}{:<15}{:<15}'.format(date, time, play, tickets, reserve))
印刷品:
Date Time Play Tickets
Th 03. 09. 2020 19:00 Racek 4 reserve
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句