python - Python Beautiful Soup 遍历多个表
问题描述
尝试使用 CSS 名称查找多个表,但我最初只在输出中获取 CSS。我想遍历每个小表,并从那里每行包含玩家信息以及每个玩家的 tds 属性。为什么我所拥有的实际上并没有打印表格内容?我想确认我已经迈出了正确的第一步,然后再继续进入每个迷你桌子的 tr 和 tds。我认为部分问题在于第一张桌子。
我的程序 -
import requests
from bs4 import BeautifulSoup
#url = 'https://www.skysports.com/premier-league-table'
base_url = 'https://www.skysports.com'
# Squad Data
squad_url = base_url + '/liverpool-squad'
squad_r = requests.get(squad_url)
print(squad_r.status_code)
premier_squad_soup = BeautifulSoup(squad_r.text, 'html.parser')
premier_squad_table = premier_squad_soup.find_all = ('table', {'class': 'table -small no-wrap football-squad-table '})
print(premier_squad_table)
HTML -
each table looks like the following but with a different title
<table class="table -small no-wrap football-squad-table " title="Goalkeeper">
<colgroup>
<col class="" style="">
<col class="digit-4 -bp30-hdn">
<col class="digit-3 ">
<col class="digit-3 ">
<col class="digit-3 ">
</colgroup>
<thead>
<tr class="text-s -interact text-h6" style="">
<th class=" text-h4 -txt-left" title="">Goalkeeper</th>
<th class=" text-h6" title="Played">Pld</th>
<th class=" text-h6" title="Goals">G</th>
<th class=" text-h6" title="Yellow Cards ">YC</th>
<th class=" text-h6" title="Red Cards">RC</th>
</tr>
</thead>
<tbody>
<tr class="text-h6 -center">
<td>
<a href="/football/player/141016/alisson-ramses-becker">
<div class="row-table -2cols">
<span class="col span4/5 -txt-left"><h6 class=" text-h5">Alisson Ramses Becker</h6></span>
</div>
</a>
</td>
<td>
13 (0) </td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr class="text-h6 -center">
<td>
<a href="/simon-mignolet">
<div class="row-table -2cols">
<span class="col span4/5 -txt-left"><h6 class=" text-h5">Simon Mignolet</h6></span>
</div>
</a>
</td>
<td>
1 (0) </td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr class="text-h6 -center">
<td>
<a href="/football/player/153304/kamil-grabara">
<div class="row-table -2cols">
<span class="col span4/5 -txt-left"><h6 class=" text-h5">Kamil Grabara</h6></span>
</div>
</a>
</td>
<td>
1 (1) </td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
输出 - 200 ('table', {'class': 'table -small no-wrap football-squad-table '})
解决方案
必须先找到 div 才能在 div 中获取表格
Premier_squad_div = Premier_squad_soup.find('div', {'class': '-bp30-box col span1/1'}) Premier_squad_table = Premier_squad_div.find_all('table', {'class': 'table -small no-wrap football -小队表'})
推荐阅读
- python - 如何从另一个列表中的字符串创建列表。Python?
- python - 如何通过检查所有元素以及使整个嵌套列表小写来检查字符串是否在嵌套列表中?
- firebase - Firebase Firestore 是否在 React Native Expo 中工作?
- sql - 如何动态地将列名传递给查询?
- php - Authorize.Net Paypal Express PHP API:交易未在 Authorize.Net 中更新,也未捕获正确金额的问题
- snowflake-cloud-data-platform - 我可以使用 snowflake snowsql !spool 命令覆盖现有文件吗?
- swift - 解析 HealthKit 查询的结果
- ffmpeg - 我怎样才能插入一个空的字幕
- user-interface - 在 Tkinter 中尝试 self.after() 时,整个小部件冻结/不加载
- javascript - AngularFireMessaging - 错误 this._next 不是函数