【问题标题】:Python Beautiful Soup Webscraping: Cannot get a full table to displayPython Beautiful Soup Webscraping:无法显示完整的表格
【发布时间】:2021-07-25 21:07:45
【问题描述】:

我对 python 比较陌生,这是我的第一次网络抓取。我正在尝试刮一张桌子,但只能显示第一列。我使用的是 find 方法而不是 find_all ,我很确定是什么原因造成的,但是当我使用 find_all 方法时,我无法显示任何文本。这是我正在抓取的网址:https://www.fangraphs.com/teams/mariners/stats 我正试图让顶级表(击球统计领导者)工作。我的代码如下:

from bs4 import BeautifulSoup
import requests
import time

htmlText = requests.get('https://www.fangraphs.com/teams/mariners/stats').text
soup = BeautifulSoup(htmlText, 'lxml', )
playerTable = soup.find('div', class_='team-stats-table')
input = input("Would you like to see Batting, Starting Pitching, Relief Pitching, or Fielding Stats? \n")


def BattingStats():
    print("BATTING STATS:")
    print("Player Name: ")
    for tr in playerTable.find_all('tr')[1:55]:
        tds = tr.find('td').text
        print(tds)

if input == "Batting" or "batting":
    BattingStats()

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    您可以使用列表理解从所有行中获取文本:

    import requests
    from bs4 import BeautifulSoup
    
    
    playerTable = soup.find("div", class_="team-stats-table")
    
    
    def BattingStats():
        print("BATTING STATS:")
        print("Player Name: ")
        for tr in playerTable.find_all("tr")[1:55]:
            tds = [td.text for td in tr.select("td")]
            print(tds)
    
    
    BattingStats()
    

    打印:

    BATTING STATS:
    Player Name: 
    Mitch Haniger 30 94 406 25 0 6.7% 23.4% .257 .291 .268 .323 .524 .358 133 0.2 16.4 -6.5 2.4
    Ty France 26 89 372 9 0 7.3% 16.9% .150 .314 .276 .355 .426 .341 121 0.0 9.5 -2.6 2.0
    Kyle Seager 33 97 403 18 2 8.4% 25.8% .201 .246 .215 .285 .416 .302 95 -0.3 -2.9 5.4 1.6
    
    ...
    

    pandas 的解决方案:

    import pandas as pd
    
    url = "https://www.fangraphs.com/teams/mariners/stats"
    
    df = pd.read_html(url)[7]
    print(df)
    

    打印:

                     Name  Age     G    PA   HR  SB    BB%      K%    ISO  BABIP    AVG    OBP    SLG   wOBA   wRC+  BsR   Off   Def  WAR
    0       Mitch Haniger   30    94   406   25   0   6.7%   23.4%  0.257  0.291  0.268  0.323  0.524  0.358  133.0  0.2  16.4  -6.5  2.4
    1           Ty France   26    89   372    9   0   7.3%   16.9%  0.150  0.314  0.276  0.355  0.426  0.341  121.0  0.0   9.5  -2.6  2.0
    2         Kyle Seager   33    97   403   18   2   8.4%   25.8%  0.201  0.246  0.215  0.285  0.416  0.302   95.0 -0.3  -2.9   5.4  1.6
    
    ...
    

    【讨论】:

      猜你喜欢
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2015-04-27
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多