首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Beautiful Soup遍历多个表

Python Beautiful Soup遍历多个表
EN

Stack Overflow用户
提问于 2018-10-28 05:54:18
回答 1查看 413关注 0票数 0

尝试使用CSS名称查找多个表,但我最初只在输出中获得CSS。我想循环遍历每个小表,从那里开始,每一行都包含关于每个球员的tds属性的球员信息。为什么我在那里拥有的东西一开始实际上并不打印表格内容呢?我想确认我已经完成了正确的第一步,然后我继续并进入每个迷你桌的tr和tds。我认为问题的一部分是第一张表。

我的程序-

代码语言:javascript
复制
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 -

代码语言:javascript
复制
    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>

Output - 200 ( 'table ',{'class':‘table -small无包装足球小队-表'})

EN

回答 1

Stack Overflow用户

发布于 2018-10-28 06:07:19

必须首先找到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无包装足球-小队-表'})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53026606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档