import requests
from bs4 import BeautifulSoup
#get website, trick website into thinking you're using chrome.
url = 'http://www.espn.com/mlb/stats/pitching/_/sort/wins/league/al/year/2019/seasontype/2'
headers ={'User-Agent': 'Mozilla/5.0'}
res = requests.get(url, headers)
#sets soup to res and spits it out in html parsing format
soup = BeautifulSoup(res.content, 'html.parser')
#finds table from website
stats = soup.find_all('table', class_='tablehead')
stats = stats[0]
#saves the table into a text file.
with open('pitchers_stats.txt', 'w') as r:
for row in stats.find_all('tr'):
r.write(row.text.ljust(5))
#delete next two lines. the program sort of works, for those trying to help me on stackOverflow.
for cell in stats.find_all('td'):
r.write('\n')
# divide each row by a new line
r.write('\n')
просто распечатывает «сортируемую передачу», и я не уверен, почему.
программа работает, как и ожидалось, удаляя строки:
for cell in stats.find_all('td'):
r.write('\n')
^ делает это показывает следующее:.