【问题标题】:How to parse nested table from HTML link using BeautifulSoup in Python?如何在 Python 中使用 BeautifulSoup 从 HTML 链接解析嵌套表?
【发布时间】:2019-06-13 07:04:42
【问题描述】:

全部,

我正在尝试从此链接 http://web1.ncaa.org/stats/StatsSrv/careersearch 解析表。 请注意:要在“学校/体育搜索”下进行搜索,请选择所有学校,-2005-2006 年,体育 -Football,Division I。我要解析的列是学校名称,如果您单击学校名称。将输出更多信息。从该链接/表格中,我想为每所学校解析“体育场容量”。我的问题是这样的事情可能吗?如果是,如何?我是 python 和 BeautifulSoup 的新手,如果你能提供解释那就太好了!

注意:有239个结果,

总结一下:所以基本上我想解析学校名称及其体育场容量并将其转换为 Pandas 数据框

import requests 
from bs4 import BeautifulSoup
URL = "http://web1.ncaa.org/stats/StatsSrv/careerteam"
r = requests.get(URL) 

soup = BeautifulSoup(r.content, 'html5lib') 
print(soup.prettify()) 

【问题讨论】:

    标签: python-3.x pandas beautifulsoup html-parsing html-parser


    【解决方案1】:

    我的问题是这样的事情可能吗?

    是的。

    如果是,怎么做?

    下面的代码中有很多内容。但重点是找出浏览器发出的发布请求,然后使用 Requests 进行模拟。我们可以通过检查工具中的“网络”选项卡找到正在发出的请求。

    首先我们发出“搜索”帖子请求。这给出了一个左右表。点击左边的表格可以为我们提供该地区的学校。但是如果我们仔细观察点击区域链接也是一个发布请求(我们必须使用请求来做)

    例如。点击“空军 - 伊利诺伊州东部”。给了我们一张包含该地区学校链接的表格。然后我们必须去那个学校链接并弄清楚容量。

    由于单击每个学校链接也是我们必须模拟的发布请求,这将返回学校页面。从这里我们抓取学校名称和容量。

    您可以阅读Advanced Usage of requests 了解 Session 对象,阅读Making a request 了解使用 Requests 发出请求。

    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    end_list=[]
    s = requests.Session()
    URL = "http://web1.ncaa.org/stats/StatsSrv/careersearch"
    data={'doWhat': 'teamSearch','searchOrg': 'X', 'academicYear': 2006, 'searchSport':'MFB','searchDiv': 1}
    r = s.post(URL,data=data)
    soup=BeautifulSoup(r.text,'html.parser')
    area_list=soup.find_all('table')[8].find_all('tr')
    area_count=len(area_list)#has no of areas + 1  tr 'Total Results of Search:  239'
    for idx in range(0,area_count):
        data={
        'sortOn': 0,
        'doWhat': 'showIdx',
        'playerId':'' ,'coachId': '',
        'orgId':'' ,
        'academicYear':'' ,
        'division':'' ,
        'sportCode':'' ,
        'idx': idx
        }
        r = s.post(URL,data=data)
        soup=BeautifulSoup(r.text,'html.parser')
        last_table=soup.find_all('table')[-1]#last table
        for tr in last_table.find_all('tr'):
            link_td=tr.find('td',class_="text")
            try:
                link_a=link_td.find('a')['href']
                data_params=link_a.split('(')[1][:-2].split(',')
                try:
                    #print(data_params)
                    sports_code=data_params[2].replace("'","").strip()
                    division=int(data_params[3])
                    player_coach_id=int(data_params[0])
                    academic_year=int(data_params[1])
                    org_id=int(data_params[4])
                    #print(sports_code,division,player_coach_id,academic_year,org_id)
                    data={
                    'sortOn': 0,
                    'doWhat': 'display',
                    'playerId': player_coach_id,
                    'coachId': player_coach_id,
                    'orgId': org_id,
                    'academicYear': academic_year,
                    'division':division,
                    'sportCode':sports_code,
                    'idx':''
                    }
                    url='http://web1.ncaa.org/stats/StatsSrv/careerteam'
                    r = s.post(url,data=data)
                    soup2=BeautifulSoup(r.text,'html.parser')
                    institution_name=soup2.find_all('table')[1].find_all('tr')[2].find_all('td')[1].text.strip()
                    capacity=soup2.find_all('table')[4].find_all('tr')[2].find_all('td')[1].text.strip()
                    #print([institution_name, capacity])
                    end_list.append([institution_name, capacity])
    
                except IndexError:
                    pass
    
            except AttributeError:
                pass
    #print(end_list)
    headers=['School','Capacity']
    df=pd.DataFrame(end_list, columns=headers)
    print(df)
    

    输出

                    School Capacity
    0            Air Force   46,692
    1                Akron   30,000
    2              Alabama  101,821
    3         Alabama A&M;   21,000
    4          Alabama St.   26,500
    5          Albany (NY)    8,500
    6               Alcorn   22,500
    7      Appalachian St.   30,000
    8              Arizona   55,675
    9          Arizona St.   64,248
    10     Ark.-Pine Bluff   14,500
    11            Arkansas   72,000
    12        Arkansas St.   30,708
    13     Army West Point   38,000
    14              Auburn   87,451
    15         Austin Peay   10,000
    16                 BYU   63,470
    17            Ball St.   22,500
    18              Baylor   45,140
    19     Bethune-Cookman    9,601
    20           Boise St.   36,387
    21      Boston College   44,500
    22       Bowling Green   24,000
    23               Brown   20,000
    24            Bucknell   13,100
    25             Buffalo   29,013
    26              Butler    5,647
    27            Cal Poly   11,075
    28          California   62,467
    29   Central Conn. St.    5,500
    ..                 ...      ...
    209               UCLA   91,136
    210              UConn   40,000
    211                UNI   16,324
    212               UNLV   36,800
    213          UT Martin    7,500
    214               UTEP   52,000
    215               Utah   45,807
    216           Utah St.   25,100
    217                VMI   10,000
    218         Valparaiso    5,000
    219         Vanderbilt   40,350
    220          Villanova   12,000
    221           Virginia   61,500
    222      Virginia Tech   65,632
    223             Wagner    3,300
    224        Wake Forest   31,500
    225         Washington   70,138
    226     Washington St.   32,740
    227          Weber St.   17,500
    228      West Virginia   60,000
    229      Western Caro.   13,742
    230       Western Ill.   16,368
    231        Western Ky.   22,113
    232      Western Mich.   30,200
    233     William & Mary   12,400
    234          Wisconsin   80,321
    235            Wofford   13,000
    236            Wyoming   29,181
    237               Yale   64,269
    238     Youngstown St.   20,630
    
    [239 rows x 2 columns]
    

    注意: 这将需要很长时间。我们正在抓取 >239 页。所以要有耐心。可能需要 15 分钟或更长时间。

    【讨论】:

    • 您能否解决此查询:stackoverflow.com/questions/54279547/… 我在此查询的网络数据下找不到参数
    • @Data_is_Power 我会试试的。我现在不在家。我会尽快通知你的。
    • 太棒了。再次感谢你! :)
    猜你喜欢
    • 2013-07-24
    • 2016-01-28
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 2018-10-30
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多