Python美丽汤在多个表上进行迭代

时间:2018-10-27 21:54:18

标签: python web-scraping html-table beautifulsoup

尝试使用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     (“表”,{“类”:“表-小无包裹橄榄球队表”))

1 个答案:

答案 0 :(得分:0)

必须先找到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-wrap football-squad-table'})