我刮了一张桌子,它使用 python 美丽的汤。如果有一种方法可以让我使用我现在拥有的代码在带有标签的组织良好的表格中打印数据,我只是好奇?
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 = [td.text for td in tr.select("td")]
print(tds)
BattingStats()
答案 0 :(得分:0)
您可以使用 str.format
来格式化您的输出:
import requests
from bs4 import BeautifulSoup
htmlText = requests.get("https://www.fangraphs.com/teams/mariners/stats").text
soup = BeautifulSoup(htmlText, "lxml")
playerTable = soup.find("div", class_="team-stats-table")
def BattingStats():
headers = [th.text for th in playerTable.find_all("th")]
fmt_string = " ".join(["{:<25}", *["{:<6}"] * (len(headers) - 1)])
print(fmt_string.format(*headers))
for tr in playerTable.find_all("tr")[1:55]:
tds = [td.text for td in tr.select("td")]
print(fmt_string.format(*tds))
BattingStats()
打印:
Name Age G PA HR SB BB% K% ISO BABIP AVG OBP SLG wOBA wRC+ BsR Off Def WAR
Mitch Haniger 30 96 414 25 0 6.8% 24.2% .252 .289 .263 .319 .515 .353 129 0.0 14.7 -7.6 2.1
Ty France 26 91 380 9 0 7.1% 16.8% .147 .314 .276 .355 .423 .341 121 0.4 10.0 -2.9 2.0
Kyle Seager 33 99 411 19 2 8.8% 25.8% .206 .247 .217 .290 .423 .306 98 -0.1 -1.2 4.4 1.7
J.P. Crawford 26 100 410 5 3 8.0% 16.8% .099 .316 .268 .329 .367 .308 99 -1.7 -2.3 3.1 1.5
Jake Fraley 26 40 149 7 7 22.1% 25.5% .202 .286 .237 .409 .439 .374 144 0.3 8.2 -3.4 1.0
Tom Murphy 30 60 203 8 0 10.8% 33.0% .176 .272 .205 .294 .381 .296 90 -0.2 -2.6 4.7 0.9
Dylan Moore 28 76 263 10 15 9.1% 31.6% .179 .239 .188 .274 .368 .282 81 1.6 -4.4 -0.7 0.4
Luis Torrens 25 57 203 12 0 8.4% 26.1% .239 .242 .223 .291 .462 .320 107 -0.2 1.4 -4.7 0.4
Kyle Lewis 25 36 147 5 2 10.9% 25.2% .146 .307 .246 .333 .392 .321 108 -0.4 0.9 -2.8 0.3
Cal Raleigh 24 8 29 1 0 6.9% 37.9% .185 .200 .148 .207 .333 .234 49 0.2 -1.6 2.0 0.1
Justin Dunn 25 11 3 0 0 0.0% 66.7% .333 1.000 .333 .333 .667 .416 172 0.0 0.3 0.4 0.1
Justus Sheffield 25 15 2 0 0 0.0% 50.0% .000 1.000 .500 .500 .500 .441 189 0.0 0.2 0.2 0.1
Dillon Thomas 28 4 9 0 0 0.0% 77.8% .000 .500 .111 .111 .111 .098 -43 0.5 -1.1 0.9 0.0
Eric Campbell 34 4 12 0 0 8.3% 8.3% .000 .300 .273 .333 .273 .278 79 0.2 -0.1 -0.2 0.0
Hector Santiago 33 12 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Casey Sadler 30 13 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
James Paxton 32 1 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Domingo Tapia 29 2 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Rafael Montero 30 40 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
JT Chargois 30 30 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Paul Sewald 31 31 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Brady Lail 27 2 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Yacksel Rios 28 3 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Keynan Middleton 27 26 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Kendall Graveman 30 30 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Erik Swanson 27 14 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Vinny Nittoli 30 1 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Daniel Zamora 28 4 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Anthony Misiewicz 26 43 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Drew Steckenrider 30 35 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Robert Dugger 25 11 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Yohan Ramirez 26 7 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Will Vest 26 32 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Ljay Newsome 24 7 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Wyatt Mills 26 8 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Nick Margevicius 25 5 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Aaron Fletcher 25 4 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Logan Gilbert 24 12 0 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 0.0 0.0 0.0 0.0
Chris Flexen 26 18 1 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 -100 0.0 -0.3 0.1 0.0
Marco Gonzales 29 13 2 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 -100 0.0 -0.5 0.2 0.0
Darren McCaughan 25 2 2 0 0 0.0% 0.0% .000 .000 .000 .000 .000 .000 -100 0.0 -0.5 0.2 0.0
Yusei Kikuchi 30 18 2 0 0 0.0% 100.0% .000 .000 .000 .000 .000 .000 -100 0.0 -0.5 0.2 0.0
Donovan Walton 27 23 69 2 1 5.8% 21.7% .159 .239 .206 .254 .365 .268 72 0.2 -2.2 -0.5 0.0
Taylor Trammell 23 51 178 8 2 9.6% 42.1% .199 .233 .160 .256 .359 .271 74 0.0 -5.7 -0.9 -0.1
Braden Bishop 27 8 5 0 0 0.0% 40.0% .000 .500 .250 .250 .250 .220 40 0.1 -0.3 -0.5 -0.1
Jacob Nottingham 26 10 31 1 0 6.5% 38.7% .115 .143 .115 .226 .231 .214 35 0.0 -2.5 0.1 -0.1
Jack Mayfield 30 12 35 0 0 2.9% 28.6% .029 .250 .176 .200 .206 .181 14 -1.2 -4.9 1.8 -0.2
Jose Godoy 26 16 40 0 0 7.5% 35.0% .027 .261 .162 .225 .189 .193 22 -0.4 -4.2 0.3 -0.3
Jake Bauers 25 33 119 1 1 5.9% 26.1% .045 .325 .241 .286 .286 .257 64 0.2 -5.0 -2.1 -0.3
Shed Long Jr. 25 32 113 4 1 7.1% 31.0% .173 .246 .192 .250 .365 .267 71 -0.9 -4.8 -3.0 -0.4
Sam Haggerty 27 35 94 2 5 6.4% 29.8% .105 .250 .186 .247 .291 .241 53 0.0 -5.3 -2.0 -0.4
Jose Marmolejos 28 31 94 3 0 14.9% 34.0% .139 .178 .139 .266 .278 .251 60 0.0 -4.5 -3.5 -0.5
Evan White 25 30 104 2 0 5.8% 29.8% .093 .188 .144 .202 .237 .198 25 0.5 -8.9 -1.2 -0.7
Jarred Kelenic 21 33 131 2 3 9.2% 33.6% .068 .139 .102 .191 .169 .173 8 1.4 -13.3 -2.5 -1.1