首页 > 解决方案 > 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 '})

标签: pythonweb-scrapinghtml-tablebeautifulsoup

解决方案


必须先找到 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 -小队表'})


推荐阅读