Reading Data from baseball-reference.com using BeautifulSoup in Python

Read Player Standard Batting table from baseball-reference.com

In [1]:
import pandas as pd
import os.path
import requests
from bs4 import BeautifulSoup

def getStandardBatting(league, year):
    csvfile = league + '_' + str(year) + '-standard-batting.csv'
    if os.path.isfile(csvfile):
        dd = pd.read_csv(csvfile)
    else:
        r = requests.get("https://www.baseball-reference.com/leagues/"+league+"/"+str(year)+"-standard-batting.shtml")
        soup = BeautifulSoup(r.content, "html.parser") # try lxml
        div = soup.find('div', id='all_players_standard_batting')
        bdiv = bytearray(str(div.contents),'utf-8')
        soup2 = BeautifulSoup(bdiv, "html.parser")
        tbody = soup2.find('tbody')
        rows = tbody.find_all('tr')
        columns = ['Name','Age','Tm','G','PA','AB','R','H','2B','3B','HR','RBI','SB','CS','BB','SO',
                   'BA','OBP','SLG','OPS','OPS+','TB','GDP','HBP','SH','SF','IBB','Pos Summary']
        dd = pd.DataFrame(columns = columns)
        irow = 0
        for row in rows:
            cols = row.find_all('td')
            if (len(cols) >= 28):
                dd.loc[irow] = [
                    cols[0].text.strip(),
                    cols[1].text.strip(),
                    cols[2].text.strip(),
                    cols[3].text.strip(),
                    cols[4].text.strip(),
                    cols[5].text.strip(),
                    cols[6].text.strip(),
                    cols[7].text.strip(),
                    cols[8].text.strip(),
                    cols[9].text.strip(),
                    cols[10].text.strip(),
                    cols[11].text.strip(),
                    cols[12].text.strip(),
                    cols[13].text.strip(),
                    cols[14].text.strip(),
                    cols[15].text.strip(),
                    cols[16].text.strip(),
                    cols[17].text.strip(),
                    cols[18].text.strip(),
                    cols[19].text.strip(),
                    cols[20].text.strip(),
                    cols[21].text.strip(),
                    cols[22].text.strip(),
                    cols[23].text.strip(),
                    cols[24].text.strip(),
                    cols[25].text.strip(),
                    cols[26].text.strip(),
                    cols[27].text.strip()
                ]
                irow = irow+1
        dd.index += 1
        dd.to_csv(csvfile)
    return dd

nl17 = getStandardBatting('NL', 2017)
al17 = getStandardBatting('AL', 2017)
print(nl17)
print(al17)
print(nl17.iloc[0,:])
print(al17.iloc[0,:])
     Unnamed: 0                  Name   Age   Tm    G   PA   AB    R    H  2B  \
0             1     Cristhian Adames#  25.0  COL   12   14   13    1    0   0   
1             2          Austin Adams  26.0  WSN    6    0    0    0    0   0   
2             3            Lane Adams  27.0  ATL   85  122  109   19   30   4   
3             4           Matt Adams*  28.0  TOT  131  367  339   46   93  22   
4             5           Matt Adams*  28.0  STL   31   53   48    4   14   2   
5             6           Matt Adams*  28.0  ATL  100  314  291   42   79  20   
6             7           Tim Adleman  29.0  CIN   27   32   29    0    3   1   
7             8         Jesus Aguilar  27.0  MIL  133  311  279   40   74  15   
8             9            Nick Ahmed  27.0  ARI   53  178  167   24   42   8   
9            10          Matt Albers*  34.0  WSN   59    0    0    0    0   0   
10           11         Ozzie Albies#  20.0  ATL   57  244  217   34   62   9   
11           12  Arismendy Alcantara#  25.0  CIN   70  108  105   13   18   3   
12           13       Sandy Alcantara  21.0  STL    8    0    0    0    0   0   
13           14          Jorge Alfaro  24.0  PHI   29  114  107   12   34   6   
14           15         Albert Almora  23.0  CHC  132  323  299   39   89  18   
15           16         Aaron Altherr  26.0  PHI  107  412  372   58  101  24   
16           17     Henderson Alvarez  27.0  PHI    3    6    6    0    0   0   
17           18       Alexi Amarista*  28.0  COL   96  176  168   22   40  10   
18           19       Brett Anderson*  29.0  CHC    6    8    7    0    1   0   
19           20        Brian Anderson  24.0  MIA   25   95   84   11   22   7   
20           21        Chase Anderson  29.0  MIL   24   55   52    1    7   1   
21           22         Drew Anderson  23.0  PHI    1    0    0    0    0   0   
22           23       Tyler Anderson*  27.0  COL   17   33   26    1    1   0   
23           24       Norichika Aoki*  35.0  NYM   27  116  102   16   29   7   
24           25          Victor Arano  22.0  PHI   10    0    0    0    0   0   
25           26         Orlando Arcia  22.0  MIL  153  548  506   56  140  17   
26           27         Nolan Arenado  26.0  COL  159  680  606  100  187  43   
27           28          Jake Arrieta  31.0  CHC   29   64   61    2    8   0   
28           29        Bronson Arroyo  40.0  CIN   15   29   26    0    4   0   
29           30      Christian Arroyo  22.0  SFG   34  135  125    9   24   5   
..          ...                   ...   ...  ...  ...  ...  ...  ...  ...  ..   
717         718         Zack Wheeler*  27.0  NYM   17   29   27    1    2   1   
718         719         Matt Wieters#  31.0  WSN  123  465  422   43   95  20   
719         720        Tom Wilhelmsen  33.0  ARI   27    1    1    0    0   0   
720         721            Adam Wilk*  29.0  NYM    1    1    1    0    0   0   
721         722       Aaron Wilkerson  28.0  MIL    3    4    4    0    0   0   
722         723        Nick Williams*  23.0  PHI   83  343  313   45   90  14   
723         724      Taylor Williams#  25.0  MIL    5    0    0    0    0   0   
724         725       Trevor Williams  25.0  PIT   30   47   41    1    2   0   
725         726        Mac Williamson  26.0  SFG   28   73   68    8   16   2   
726         727        Justin Wilson*  29.0  CHC   22    0    0    0    0   0   
727         728         Jesse Winker*  23.0  CIN   47  137  121   21   36   7   
728         729           Dan Winkler  27.0  ATL   16    0    0    0    0   0   
729         730           Matt Wisler  24.0  ATL   20    3    2    0    0   0   
730         731         Nick Wittgren  26.0  MIA   34    4    4    0    0   0   
731         732   Asher Wojciechowski  28.0  CIN   23   14   14    0    1   0   
732         733         Tony Wolters*  25.0  COL   83  266  229   30   55   8   
733         734          Kolten Wong*  26.0  STL  108  411  354   55  101  27   
734         735             Alex Wood  26.0  LAD   27   58   52    1    3   0   
735         736            Blake Wood  31.0  CIN   52    1    1    0    0   0   
736         737           Travis Wood  30.0  SDP   11   17   17    3    4   0   
737         738     Brandon Woodruff*  24.0  MIL    7   12   10    2    2   0   
738         739          Vance Worley  29.0  MIA   25   27   22    0    4   0   
739         740          Kirby Yates*  30.0  SDP   55    0    0    0    0   0   
740         741     Christian Yelich*  25.0  MIA  156  695  602  100  170  36   
741         742          Mark Zagunis  24.0  CHC    7   18   14    0    0   0   
742         743         Rob Zastryzny  25.0  CHC    4    3    3    0    1   0   
743         744          Brad Ziegler  37.0  MIA   51    0    0    0    0   0   
744         745        Ryan Zimmerman  32.0  WSN  144  576  524   90  159  33   
745         746          Ben Zobrist#  36.0  CHC  128  496  435   58  101  20   
746         747      LgAvg per 600 PA   NaN  NaN  230  600  534   72  136  27   

        ...         SLG    OPS   OPS+   TB  GDP  HBP  SH  SF  IBB  Pos Summary  
0       ...       0.000  0.071  -80.0    0    0    0   0   0    0         /346  
1       ...         NaN    NaN    NaN    0    0    0   0   0    0           /1  
2       ...       0.468  0.807  108.0   51    3    1   1   1    0         78/9  
3       ...       0.522  0.841  115.0  177    5    1   0   4    5         37/D  
4       ...       0.396  0.735   95.0   19    0    0   0   1    0         /73D  
5       ...       0.543  0.858  118.0  158    5    1   0   3    5           37  
6       ...       0.138  0.241  -37.0    4    0    0   3   0    0            1  
7       ...       0.505  0.837  114.0  141    8    4   0   3    1         3/D5  
8       ...       0.419  0.717   78.0   70    6    1   0   0    3            6  
9       ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
10      ...       0.456  0.810  110.0   99    3    3   1   2    0            4  
11      ...       0.248  0.435   13.0   26    2    0   1   0    0      4/89657  
12      ...         NaN    NaN    NaN    0    0    0   0   0    0           /1  
13      ...       0.514  0.874  129.0   55    2    4   0   0    1          2/3  
14      ...       0.445  0.782  101.0  133    8    0   3   2    1         8/79  
15      ...       0.516  0.856  124.0  192   12    7   0   1    2          798  
16      ...       0.000  0.000 -100.0    0    0    0   0   0    0           /1  
17      ...       0.351  0.620   52.0   59    5    0   1   0    0      46/8975  
18      ...       0.143  0.393    7.0    1    0    0   0   0    0           /1  
19      ...       0.369  0.706   92.0   31    1    0   0   1    0            5  
20      ...       0.154  0.288  -25.0    8    1    0   3   0    0            1  
21      ...         NaN    NaN    NaN    0    0    0   0   0    0           /1  
22      ...       0.038  0.113  -71.0    1    1    0   6   0    0            1  
23      ...       0.373  0.743   99.0   38    1    1   0   0    0         9/78  
24      ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
25      ...       0.407  0.731   89.0  206   10    1   2   3    9           *6  
26      ...       0.586  0.959  132.0  355   21    4   1   6    9           *5  
27      ...       0.213  0.358   -9.0   13    0    0   2   0    0            1  
28      ...       0.154  0.308  -19.0    4    0    0   3   0    0            1  
29      ...       0.304  0.548   45.0   38    4    1   0   1    1         56/4  
..      ...         ...    ...    ...  ...  ...  ...  ..  ..  ...          ...  
717     ...       0.111  0.218  -42.0    3    0    0   1   0    0            1  
718     ...       0.344  0.632   64.0  145   14    1   0   4    4           *2  
719     ...       0.000  0.000 -100.0    0    0    0   0   0    0            1  
720     ...       0.000  0.000 -100.0    0    0    0   0   0    0           /1  
721     ...       0.000  0.000 -100.0    0    0    0   0   0    0           /1  
722     ...       0.473  0.811  113.0  148    8    6   0   4    0        987/D  
723     ...         NaN    NaN    NaN    0    0    0   0   0    0           /1  
724     ...       0.049  0.142  -61.0    2    1    0   4   0    0            1  
725     ...       0.397  0.685   79.0   27    2    0   0   0    1         9/7D  
726     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
727     ...       0.529  0.904  133.0   64    2    0   1   0    0         9/D7  
728     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
729     ...       0.000  0.000 -100.0    0    0    0   1   0    0            1  
730     ...       0.000  0.000 -100.0    0    0    0   0   0    0            1  
731     ...       0.071  0.143  -62.0    1    0    0   0   0    0            1  
732     ...       0.284  0.625   58.0   65    9    2   2   0    9         2/45  
733     ...       0.412  0.788  110.0  146    4   12   1   3   11            4  
734     ...       0.058  0.150  -59.0    3    1    0   4   0    0            1  
735     ...       0.000  0.000 -100.0    0    0    0   0   0    0            1  
736     ...       0.588  0.824  111.0   10    0    0   0   0    0            1  
737     ...       0.200  0.473   27.0    2    0    0   1   0    0           /1  
738     ...       0.182  0.364   -1.0    4    1    0   5   0    0            1  
739     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
740     ...       0.439  0.807  118.0  264   13    6   0   6    4           *8  
741     ...       0.000  0.222  -34.0    0    0    0   0   0    0           /9  
742     ...       0.333  0.667   75.0    1    0    0   0   0    0           /1  
743     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
744     ...       0.573  0.930  136.0  300   16    3   0   5    1         *3/D  
745     ...       0.375  0.693   79.0  163   13    2   2   3    2       479/36  
746     ...       0.423  0.748    NaN  226   12    6   4   4    4          NaN  

[747 rows x 29 columns]
     Unnamed: 0               Name   Age   Tm    G   PA   AB    R    H  2B  \
0             1     Fernando Abad*  31.0  BOS    4    0    0    0    0   0   
1             2         Jose Abreu  30.0  CHW  156  675  621   95  189  43   
2             3        Jim Adduci*  32.0  DET   29   93   83   14   20   6   
3             4    Ehire Adrianza#  27.0  MIN   70  186  162   30   43   9   
4             5      Andrew Albers  31.0  SEA    1    3    1    0    1   0   
5             6    Al Alburquerque  31.0  TOT    1    0    0    0    0   0   
6             7    Al Alburquerque  31.0  KCR    1    0    0    0    0   0   
7             8   Scott Alexander*  27.0  KCR    4    3    3    0    0   0   
8             9     Anthony Alford  22.0  TOR    4    8    8    0    1   1   
9            10         Cody Allen  28.0  CLE    5    0    0    0    0   0   
10           11        Greg Allen#  24.0  CLE   25   39   35    7    8   1   
11           12   Abraham Almonte#  28.0  CLE   69  195  172   26   40   8   
12           13     Yonder Alonso*  30.0  TOT  142  521  451   72  120  22   
13           14     Yonder Alonso*  30.0  OAK  100  371  319   52   85  17   
14           15     Yonder Alonso*  30.0  SEA   42  150  132   20   35   5   
15           16      Dan Altavilla  24.0  SEA    5    0    0    0    0   0   
16           17        Jose Altuve  27.0  HOU  153  662  590  112  204  39   
17           18      Jose Alvarez*  28.0  LAA    5    0    0    0    0   0   
18           19     Pedro Alvarez*  30.0  BAL   14   34   32    4   10   1   
19           20       Tim Anderson  24.0  CHW  146  606  587   72  151  26   
20           21       Elvis Andrus  28.0  TEX  158  689  643  100  191  44   
21           22     Miguel Andujar  22.0  NYY    5    8    7    0    4   2   
22           23    Norichika Aoki*  35.0  TOT   82  258  234   32   64  13   
23           24    Norichika Aoki*  35.0  HOU   70  224  202   28   55  12   
24           25    Norichika Aoki*  35.0  TOR   12   34   32    4    9   1   
25           26     Jayson Aquino*  24.0  BAL    1    2    2    0    0   0   
26           27       Chris Archer  28.0  TBR    3    8    8    1    2   0   
27           28    Shawn Armstrong  26.0  CLE    3    0    0    0    0   0   
28           29        Cody Asche*  27.0  CHW   19   62   57    5    6   1   
29           30         Alec Asher  25.0  BAL    2    1    1    0    0   0   
..          ...                ...   ...  ...  ...  ...  ...  ...  ...  ..   
610         611   Justin Verlander  34.0  DET    2    6    6    0    1   0   
611         612       Nick Vincent  30.0  SEA    6    0    0    0    0   0   
612         613  Daniel Vogelbach*  24.0  SEA   16   31   28    0    6   1   
613         614      Stephen Vogt*  32.0  OAK   54  174  157   12   34   8   
614         615        Tyler Wade*  22.0  NYY   30   63   58    7    9   4   
615         616        Adam Warren  29.0  NYY    4    0    0    0    0   0   
616         617  David Washington*  26.0  BAL    3    6    6    0    0   0   
617         618       Rickie Weeks  34.0  TBR   37  112   97   13   21   6   
618         619       Joey Wendle*  27.0  OAK    8   14   13    3    4   1   
619         620        Tyler White  26.0  HOU   22   67   61    7   17   6   
620         621      Chase Whitley  28.0  TBR    4    0    0    0    0   0   
621         622    Mason Williams*  25.0  NYY    5   17   16    3    4   0   
622         623        Alex Wilson  30.0  DET    6    0    0    0    0   0   
623         624     Justin Wilson*  29.0  DET    2    0    0    0    0   0   
624         625       Tyler Wilson  27.0  BAL    2    0    0    0    0   0   
625         626      Alex Wimmers*  28.0  MIN    1    0    0    0    0   0   
626         627        Travis Wood  30.0  KCR    3    1    1    0    0   0   
627         628    Brandon Workman  28.0  BOS    1    0    0    0    0   0   
628         629        Mike Wright  27.0  BAL    2    0    0    0    0   0   
629         630    Jimmy Yacabonis  25.0  BAL    2    2    2    0    0   0   
630         631       Gabriel Ynoa  24.0  BAL    1    1    1    0    0   0   
631         632       Michael Ynoa  25.0  CHW    1    0    0    0    0   0   
632         633        Chris Young  38.0  KCR    2    0    0    0    0   0   
633         634        Chris Young  33.0  BOS   90  276  243   30   57  12   
634         635        Eric Young#  32.0  LAA   47  125  110   24   29   5   
635         636    Bradley Zimmer*  24.0  CLE  101  332  299   41   72  15   
636         637  Jordan Zimmermann  31.0  DET    3    6    6    0    1   0   
637         638        Mike Zunino  26.0  SEA  124  435  387   52   97  25   
638         639          Tony Zych  26.0  SEA    4    0    0    0    0   0   
639         640   LgAvg per 600 PA   NaN  NaN  166  600  538   74  137  27   

        ...         SLG    OPS   OPS+   TB  GDP  HBP  SH  SF  IBB  Pos Summary  
0       ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
1       ...       0.552  0.906  141.0  343   21   15   0   4    6          *3D  
2       ...       0.398  0.720   90.0   33    1    0   0   0    0          9/D  
3       ...       0.383  0.707   89.0   62    0    1   1   6    1      67/453D  
4       ...       1.000  2.000  448.0    1    0    0   2   0    0           /1  
5       ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
6       ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
7       ...       0.000  0.000 -100.0    0    0    0   0   0    0            1  
8       ...       0.250  0.375   -5.0    2    0    0   0   0    0          /79  
9       ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
10      ...       0.343  0.625   63.0   12    0    1   0   1    0          8/7  
11      ...       0.366  0.681   78.0   63    2    1   1   1    0        97/8D  
12      ...       0.501  0.866  133.0  226    9    2   0   0    6         *3/D  
13      ...       0.527  0.896  141.0  168    6    2   0   0    6          3/D  
14      ...       0.439  0.793  114.0   58    3    0   0   0    0            3  
15      ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
16      ...       0.547  0.957  164.0  323   19    9   1   4    3         *4/D  
17      ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
18      ...       0.438  0.790  112.0   14    0    0   0   0    0          /D3  
19      ...       0.402  0.679   81.0  236   13    3   2   1    0           *6  
20      ...       0.471  0.808  108.0  303   18    3   1   4    0         *6/D  
21      ...       0.857  1.482  286.0    6    0    0   0   0    0          /5D  
22      ...       0.402  0.721   98.0   94   11    2   1   5    1       79/8D1  
23      ...       0.371  0.694   93.0   75   11    2   1   4    1        79/D1  
24      ...       0.594  0.888  124.0   19    0    0   0   1    0         /978  
25      ...       0.000  0.000 -100.0    0    0    0   0   0    0           /1  
26      ...       0.250  0.500   39.0    2    1    0   0   0    0            1  
27      ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
28      ...       0.175  0.353   -3.0   10    1    2   0   0    1        D/357  
29      ...       0.000  0.000 -100.0    0    0    0   0   0    0            1  
..      ...         ...    ...    ...  ...  ...  ...  ..  ..  ...          ...  
610     ...       0.167  0.333  -11.0    1    0    0   0   0    0            1  
611     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
612     ...       0.250  0.540   50.0    7    2    0   0   0    0          /3D  
613     ...       0.357  0.644   75.0   56    1    0   0   1    1         2/D7  
614     ...       0.224  0.446   19.0   13    2    0   0   0    0       4/679D  
615     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
616     ...       0.000  0.000 -100.0    0    0    0   0   0    0          /9D  
617     ...       0.340  0.662   84.0   33    0    3   0   0    0           D3  
618     ...       0.615  0.973  159.0    8    0    0   0   0    1           /4  
619     ...       0.525  0.853  132.0   32    0    1   0   1    0        3/471  
620     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
621     ...       0.250  0.544   46.0    4    0    0   0   0    0          /89  
622     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
623     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
624     ...         NaN    NaN    NaN    0    0    0   0   0    0           /1  
625     ...         NaN    NaN    NaN    0    0    0   0   0    0           /1  
626     ...       0.000  0.000 -100.0    0    1    0   0   0    0            1  
627     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
628     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
629     ...       0.000  0.000 -100.0    0    0    0   0   0    0            1  
630     ...       0.000  0.000 -100.0    0    0    0   0   0    0           /1  
631     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
632     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
633     ...       0.387  0.709   84.0   94    4    2   0   1    0         7D/9  
634     ...       0.418  0.754  103.0   46    0    7   3   0    0        78/D9  
635     ...       0.385  0.692   80.0  115    5    4   0   3    1            8  
636     ...       0.167  0.333  -11.0    1    0    0   0   0    0            1  
637     ...       0.509  0.840  123.0  197    8    8   0   1    0         *2/D  
638     ...         NaN    NaN    NaN    0    0    0   0   0    0            1  
639     ...       0.428  0.752    NaN  230   13    6   2   4    2          NaN  

[640 rows x 29 columns]
Unnamed: 0                     1
Name           Cristhian Adames#
Age                           25
Tm                           COL
G                             12
PA                            14
AB                            13
R                              1
H                              0
2B                             0
3B                             0
HR                             0
RBI                            0
SB                             0
CS                             0
BB                             1
SO                             6
BA                             0
OBP                        0.071
SLG                            0
OPS                        0.071
OPS+                         -80
TB                             0
GDP                            0
HBP                            0
SH                             0
SF                             0
IBB                            0
Pos Summary                 /346
Name: 0, dtype: object
Unnamed: 0                  1
Name           Fernando Abad*
Age                        31
Tm                        BOS
G                           4
PA                          0
AB                          0
R                           0
H                           0
2B                          0
3B                          0
HR                          0
RBI                         0
SB                          0
CS                          0
BB                          0
SO                          0
BA                        NaN
OBP                       NaN
SLG                       NaN
OPS                       NaN
OPS+                      NaN
TB                          0
GDP                         0
HBP                         0
SH                          0
SF                          0
IBB                         0
Pos Summary                 1
Name: 0, dtype: object

Read Player Starter Pitching table from baseball-reference.com

In [2]:
import pandas as pd
import os.path
import requests
from bs4 import BeautifulSoup

def getStarterPitching(league, year):
    csvfile = league + '_' + str(year) + '-starter-pitching.csv'
    if os.path.isfile(csvfile):
        dd = pd.read_csv(csvfile)
    else:
        r = requests.get("https://www.baseball-reference.com/leagues/"+league+"/"+str(year)+"-starter-pitching.shtml")
        soup = BeautifulSoup(r.content, "html.parser") # try lxml
        div = soup.find('div', id='all_players_starter_pitching')
        bdiv = bytearray(str(div.contents),'utf-8')
        soup2 = BeautifulSoup(bdiv, "html.parser")
        tbody = soup2.find('tbody')
        rows = tbody.find_all('tr')
        columns = ['Name','Age','Tm','IP','G','GS','Wgs','Lgs','ND','Wchp','Ltuf','Wtm','Ltm','tmW-L%','Wist','Lsv',
                   'CG','SHO','QS','QS%','GmScA','Best','Wrst','BQR','BQS','sDR','IDR','RS/GS','RS/IP','IP/GS',
                   'Pit/GS','<80','80-99','100-119','≥120','Max']
        dd = pd.DataFrame(columns = columns)
        irow = 0
        for row in rows:
            cols = row.find_all('td')
            if (len(cols) >= 28):
                dd.loc[irow] = [
                    cols[0].text.strip(),
                    cols[1].text.strip(),
                    cols[2].text.strip(),
                    cols[3].text.strip(),
                    cols[4].text.strip(),
                    cols[5].text.strip(),
                    cols[6].text.strip(),
                    cols[7].text.strip(),
                    cols[8].text.strip(),
                    cols[9].text.strip(),
                    cols[10].text.strip(),
                    cols[11].text.strip(),
                    cols[12].text.strip(),
                    cols[13].text.strip(),
                    cols[14].text.strip(),
                    cols[15].text.strip(),
                    cols[16].text.strip(),
                    cols[17].text.strip(),
                    cols[18].text.strip(),
                    cols[19].text.strip(),
                    cols[20].text.strip(),
                    cols[21].text.strip(),
                    cols[22].text.strip(),
                    cols[23].text.strip(),
                    cols[24].text.strip(),
                    cols[25].text.strip(),
                    cols[26].text.strip(),
                    cols[27].text.strip(),
                    cols[28].text.strip(),
                    cols[29].text.strip(),
                    cols[30].text.strip(),
                    cols[31].text.strip(),
                    cols[32].text.strip(),
                    cols[33].text.strip(),
                    cols[34].text.strip(),
                    cols[35].text.strip()
                ]
                irow = irow+1
        dd.index += 1
        dd.to_csv(csvfile)
    return dd

nl17 = getStarterPitching('NL', 2017)
al17 = getStarterPitching('AL', 2017)
print(nl17)
print(al17)
print(nl17.iloc[0,:])
print(al17.iloc[0,:])
     Unnamed: 0                 Name  Age   Tm     IP   G  GS  Wgs  Lgs  ND  \
0             1          Tim Adleman   29  CIN  122.1  30  20    5    9   6   
1             2    Henderson Alvarez   27  PHI   14.2   3   3    0    1   2   
2             3      Brett Anderson*   29  CHC   22.0   6   6    2    2   2   
3             4       Chase Anderson   29  MIL  141.1  25  25   12    4   9   
4             5      Tyler Anderson*   27  COL   86.0  17  15    5    6   4   
5             6         Jake Arrieta   31  CHC  168.1  30  30   14   10   6   
6             7       Bronson Arroyo   40  CIN   71.0  14  14    3    6   5   
7             8         Homer Bailey   31  CIN   91.0  18  18    6    9   3   
8             9       Anthony Banda*   23  ARI   25.2   8   4    1    3   0   
9            10          Chad Bettis   28  COL   46.1   9   9    2    4   3   
10           11            Ty Blach*   26  SFG  163.2  34  24    8   12   4   
11           12          Aaron Blair   25  ATL    3.0   1   1    0    1   0   
12           13       Michael Blazek   28  MIL    8.2   5   1    0    1   0   
13           14   Lisalverto Bonilla   27  CIN   36.2  10   4    0    3   1   
14           15       Steven Brault*   25  PIT   34.2  11   4    1    0   3   
15           16        Clay Buchholz   32  PHI    7.1   2   2    0    1   1   
16           17   Madison Bumgarner*   27  SFG  111.0  17  17    4    9   4   
17           18         Eddie Butler   26  CHC   54.2  13  11    4    3   4   
18           19        Trevor Cahill   29  SDP   61.0  11  11    4    3   4   
19           20            Matt Cain   32  SFG  124.1  27  23    3   11   9   
20           21        Luis Castillo   24  CIN   89.1  15  15    3    7   5   
21           22       Jhoulys Chacin   29  SDP  180.1  32  32   13   10   9   
22           23       Tyler Chatwood   27  COL  147.2  33  25    8   14   3   
23           24        Wei-Yin Chen*   31  MIA   33.0   9   5    2    1   2   
24           25            A.J. Cole   25  WSN   52.0  11   8    3    5   0   
25           26          Gerrit Cole   26  PIT  203.0  33  33   12   12   9   
26           27        Bartolo Colon   44  ATL   63.0  13  13    2    8   3   
27           28         Adam Conley*   27  MIA  102.2  22  20    7    7   6   
28           29      Patrick Corbin*   27  ARI  189.2  33  32   14   13   5   
29           30        Jarred Cosart   27  SDP   24.0   7   6    0    2   4   
..          ...                  ...  ...  ...    ...  ..  ..  ...  ...  ..   
131         132          Dan Straily   28  MIA  181.2  33  33   10    9  14   
132         133    Stephen Strasburg   28  WSN  175.1  28  28   15    4   9   
133         134       Chris Stratton   26  SFG   58.2  13  10    4    3   3   
134         135       Ross Stripling   27  LAD   74.1  49   2    0    0   2   
135         136         Brent Suter*   27  MIL   81.2  22  14    3    2   9   
136         137     Noah Syndergaard   24  NYM   30.1   7   7    1    2   4   
137         138      Jameson Taillon   25  PIT  133.2  25  25    8    7  10   
138         139        Julio Teheran   26  ATL  188.1  32  32   11   13   8   
139         140        Jake Thompson   23  PHI   46.1  11   8    3    2   3   
140         141         Jen-Ho Tseng   22  CHC    6.0   2   1    0    0   1   
141         142         Jacob Turner   26  WSN   39.0  18   2    0    1   1   
142         143           Jose Urena   25  MIA  169.2  34  28   14    7   7   
143         144         Julio Urias*   20  LAD   23.1   5   5    0    2   3   
144         145      Vince Velasquez   25  PHI   72.0  15  15    2    7   6   
145         146      Edinson Volquez   33  MIA   92.1  17  17    4    8   5   
146         147        Michael Wacha   25  STL  165.2  30  30   12    9   9   
147         148      Adam Wainwright   35  STL  123.1  24  23   12    5   6   
148         149       Taijuan Walker   24  ARI  157.1  28  28    9    9  10   
149         150         Jered Weaver   34  SDP   42.1   9   9    0    5   4   
150         151          Luke Weaver   23  STL   60.1  13  10    7    2   1   
151         152         Zack Wheeler   27  NYM   86.1  17  17    3    7   7   
152         153           Adam Wilk*   29  NYM    3.2   1   1    0    1   0   
153         154      Aaron Wilkerson   28  MIL   10.1   3   2    1    0   1   
154         155      Trevor Williams   25  PIT  150.1  31  25    6    8  11   
155         156          Matt Wisler   24  ATL   32.1  20   1    0    1   0   
156         157  Asher Wojciechowski   28  CIN   62.1  25   8    2    3   3   
157         158           Alex Wood*   26  LAD  152.1  27  25   15    3   7   
158         159         Travis Wood*   30  SDP   52.1  11  11    3    4   4   
159         160     Brandon Woodruff   24  MIL   43.0   8   8    2    3   3   
160         161         Vance Worley   29  MIA   71.2  24  12    2    5   5   

    ...   IDR  RS/GS  RS/IP  IP/GS  Pit/GS  <80  80-99  100-119  ≥120  Max  
0   ...     8    5.1    4.8    5.3      90    3     13        4     0  110  
1   ...     3    2.8    4.3    4.9      87    0      3        0     0   89  
2   ...     4    6.0    7.3    3.7      68    3      2        1     0  100  
3   ...    11    4.7    4.5    5.7      90    3     16        6     0  114  
4   ...     5    5.8    5.3    5.4      88    4      8        3     0  103  
5   ...    16    5.2    5.2    5.6      91    3     21        6     0  111  
6   ...     6    5.4    4.4    5.1      83    3     11        0     0   95  
7   ...     8    4.8    5.0    5.1      88    6      7        5     0  117  
8   ...     3    3.8    1.9    4.9      84    1      3        0     0   92  
9   ...     4    3.3    3.6    5.1      82    2      7        0     0   95  
10  ...    10    4.1    4.5    6.2      96    2     12       10     0  112  
11  ...     1    3.0    3.0    3.0      75    1      0        0     0   75  
12  ...     0    2.0    0.0    2.3      61    1      0        0     0   61  
13  ...     2    3.3    2.1    5.2      88    1      2        1     0  105  
14  ...     3    7.3    7.6    4.5      79    2      2        0     0   92  
15  ...     1    4.0    6.4    3.7      69    2      0        0     0   77  
16  ...    10    2.8    2.6    6.5      98    0     10        7     0  114  
17  ...     6    3.9    3.5    4.7      80    6      5        0     0   94  
18  ...     6    4.8    4.0    5.5      94    0      8        3     0  111  
19  ...    12    4.0    2.7    5.2      85    7     14        2     0  112  
20  ...     5    4.3    3.6    6.0      99    0      7        8     0  113  
21  ...    20    3.7    2.7    5.6      92    3     20        9     0  108  
22  ...     9    4.2    3.9    5.5      91    4     13        8     0  114  
23  ...     3    4.5    6.3    5.4      84    1      3        1     0  100  
24  ...     6    3.1    2.6    5.5     100    0      3        5     0  109  
25  ...    17    4.2    3.8    6.2     100    2     11       20     0  115  
26  ...     7    5.1    3.7    4.8      84    3      9        1     0  101  
27  ...     9    5.4    5.5    5.0      85    5      9        6     0  111  
28  ...    16    5.4    4.6    5.8      95    5     14       13     0  113  
29  ...     4    3.6    1.5    3.9      77    4      2        0     0   96  
..  ...   ...    ...    ...    ...     ...  ...    ...      ...   ...  ...  
131 ...    15    5.3    4.7    5.5      93    5     21        7     0  113  
132 ...    17    4.8    4.3    6.3      98    2     10       16     0  119  
133 ...     9    4.2    3.7    5.1      91    1      4        5     0  110  
134 ...     0    3.0    1.8    2.5      36    2      0        0     0   49  
135 ...     7    4.5    4.5    5.0      77    7      6        1     0  100  
136 ...     6    3.6    3.8    4.3      65    3      2        2     0  114  
137 ...    14    3.8    3.5    5.3      93    3     14        8     0  116  
138 ...    17    4.5    4.5    5.9      96    3     16       13     0  112  
139 ...     6    6.6    5.8    5.2      89    1      6        1     0  103  
140 ...     1   15.8    9.0    3.0      70    1      0        0     0   70  
141 ...     1    2.5    3.0    5.7      87    0      2        0     0   89  
142 ...    11    5.5    4.6    5.5      95    3     14       11     0  107  
143 ...     4    4.1    2.6    4.7      82    2      3        0     0   95  
144 ...     9    4.0    2.7    4.8      84    4      6        5     0  108  
145 ...     7    3.8    4.2    5.4      89    2     13        2     0  111  
146 ...    16    4.4    4.4    5.5      90    4     20        6     0  119  
147 ...    12    5.8    5.6    5.3      92    3     11        9     0  109  
148 ...    17    4.7    4.2    5.6      98    1     15       12     0  117  
149 ...     4    2.2    1.5    4.7      75    4      5        0     0   89  
150 ...     6    5.8    7.5    5.5      96    1      5        4     0  111  
151 ...    12    4.0    3.5    5.1      92    2      9        6     0  108  
152 ...     1    0.0    0.0    3.7      80    0      1        0     0   80  
153 ...     1    5.0    5.4    4.7      63    1      1        0     0   86  
154 ...    11    4.3    3.2    5.5      89    6     16        3     0  114  
155 ...     1    1.0    0.0    6.0      94    0      1        0     0   94  
156 ...     3    4.3    3.4    4.4      81    3      4        1     0  100  
157 ...    20    5.9    5.5    5.9      88    4     20        1     0  100  
158 ...     6    4.4    3.6    4.8      88    2      5        4     0  107  
159 ...     5    3.0    3.7    5.4      90    1      6        1     0  105  
160 ...     4    5.4    5.6    4.4      74    8      4        0     0   92  

[161 rows x 37 columns]
     Unnamed: 0                 Name  Age   Tm     IP   G  GS  Wgs  Lgs  ND  \
0             1       Andrew Albers*   31  SEA   41.0   9   6    4    1   1   
1             2       Raul Alcantara   24  OAK   24.0   8   4    1    2   1   
2             3      Brett Anderson*   29  TOR   33.1   7   7    2    2   3   
3             4        Matt Andriese   27  TBR   86.0  18  17    5    5   7   
4             5       Jayson Aquino*   24  BAL   13.1   4   2    1    1   0   
5             6         Chris Archer   28  TBR  201.0  34  34   10   12  12   
6             7           Alec Asher   25  BAL   60.0  24   6    1    4   1   
7             8         Trevor Bauer   26  CLE  176.1  32  31   17    9   5   
8             9           Chad Bell*   28  DET   62.1  28   4    0    2   2   
9            10    Christian Bergman   29  SEA   54.0  13   8    4    4   0   
10           11         Jose Berrios   23  MIN  145.2  26  25   13    8   4   
11           12          Joe Biagini   27  TOR  119.2  44  18    2   12   4   
12           13  Austin Bibens-Dirkx   32  TEX   69.1  24   6    2    1   3   
13           14       Paul Blackburn   23  OAK   58.2  10  10    3    1   6   
14           15       Mike Bolsinger   29  TOR   41.1  11   5    0    2   3   
15           16        Matthew Boyd*   26  DET  135.0  26  25    6   11   8   
16           17      Parker Bridwell   25  LAA  121.0  21  20   10    3   7   
17           18          Dylan Bundy   24  BAL  169.2  28  28   13    9   6   
18           19        Trevor Cahill   29  KCR   23.0  10   3    0    0   3   
19           20      Carlos Carrasco   30  CLE  200.0  32  32   18    6   8   
20           21       Andrew Cashner   30  TEX  166.2  28  28   11   11   6   
21           22        Miguel Castro   22  BAL   66.1  39   1    0    1   0   
22           23           Luis Cessa   25  NYY   36.0  10   5    0    3   2   
23           24         Jesse Chavez   33  LAA  138.0  38  21    5    9   7   
24           25        Alex Claudio*   25  TEX   82.2  70   1    0    0   1   
25           26       Mike Clevinger   26  CLE  121.2  27  21   11    4   6   
26           27            Alex Cobb   29  TBR  179.1  29  29   12   10   7   
27           28        Bartolo Colon   44  MIN   80.0  15  15    5    6   4   
28           29        Jharel Cotton   25  OAK  129.0  24  24    9   10   5   
29           30          Dylan Covey   25  CHW   70.0  18  12    0    7   5   
..          ...                  ...  ...  ...    ...  ..  ..  ...  ...  ..   
161         162         Matt Strahm*   25  KCR   34.2  24   3    1    2   0   
162         163       Marcus Stroman   26  TOR  201.0  33  33   13    9  11   
163         164      Masahiro Tanaka   28  NYY  178.1  30  30   13   12   5   
164         165         Nick Tepesch   28  TOT   15.2   4   4    1    2   1   
165         166         Nick Tepesch   28  MIN    1.2   1   1    0    1   0   
166         167         Nick Tepesch   28  TOR   14.0   3   3    1    1   1   
167         168        Chris Tillman   29  BAL   93.0  24  19    1    7  11   
168         169          Josh Tomlin   32  CLE  141.0  26  26   10    9   7   
169         170        Andrew Triggs   28  OAK   65.1  12  12    5    6   1   
170         171          Nik Turley*   27  MIN   17.2  10   3    0    2   1   
171         172         Cesar Valdez   32  TOT   30.2  11   4    1    1   2   
172         173         Cesar Valdez   32  OAK    9.1   4   1    0    0   1   
173         174         Cesar Valdez   32  TOR   21.1   7   3    1    1   1   
174         175        Jason Vargas*   34  KCR  179.2  32  32   18   11   3   
175         176     Hector Velazquez   28  BOS   24.2   8   3    0    1   2   
176         177        Drew VerHagen   26  DET   34.1  24   2    0    1   1   
177         178     Justin Verlander   34  TOT  206.0  33  33   15    8  10   
178         179     Justin Verlander   34  DET  172.0  28  28   10    8  10   
179         180     Justin Verlander   34  HOU   34.0   5   5    5    0   0   
180         181        Chris Volstad   30  CHW   19.1   6   2    0    2   0   
181         182           Ryan Weber   26  SEA    3.2   1   1    0    0   1   
182         183        Robert Whalen   23  SEA    7.1   2   1    0    1   0   
183         184           Adam Wilk*   29  MIN   10.1   3   1    0    1   0   
184         185         Tyler Wilson   27  BAL   15.1   9   1    1    0   0   
185         186         Travis Wood*   30  KCR   41.2  28   3    0    1   2   
186         187        Daniel Wright   26  LAA   19.2   5   2    0    1   1   
187         188        Steven Wright   32  BOS   24.0   5   5    1    3   1   
188         189         Gabriel Ynoa   24  BAL   34.2   9   4    1    3   0   
189         190          Chris Young   38  KCR   30.0  14   2    0    0   2   
190         191    Jordan Zimmermann   31  DET  160.0  29  29    8   13   8   

     ...    QS   QS%  GmScA  Best  Wrst  BQR  BQS  sDR  IDR  RS/GS  
0    ...     2   33%   50.3    70    25    7    5    0    5    6.0  
1    ...     0    0%   44.0    66    15    2    0    0    3    4.4  
2    ...     2   29%   46.1    64     7    5    1    0    4    3.3  
3    ...     6   35%   48.9    73    13   10    3    0   10    4.2  
4    ...     1   50%   51.0    51    51    0    0    0    2    2.1  
5    ...    20   59%   54.5    83    17   17    4    0   16    4.6  
6    ...     3   50%   43.7    67    20   20    5    1    3    2.0  
7    ...    15   48%   52.4    69    18   21    4    2   16    5.0  
8    ...     0    0%   33.5    48    21   29   13    0    2    5.0  
9    ...     3   38%   43.9    81    -8    8    2    0    2    4.8  
10   ...    12   48%   53.4    85    22   19    7    0   11    4.5  
11   ...     5   28%   44.9    76    10   16    5    1    9    2.4  
12   ...     2   33%   48.5    68    37   12    4    1    2    5.1  
13   ...     5   50%   51.2    73    29    8    4    0    5    5.4  
14   ...     0    0%   42.6    55    27   11    0    0    3    4.8  
15   ...    10   40%   45.8    89    12   19    7    1   10    4.2  
16   ...    13   65%   52.6    73    18   12    3    0    8    5.1  
17   ...    19   68%   53.4    95    25   11    7    0   15    5.2  
18   ...     0    0%   34.7    45    26    7    0    0    2    6.3  
19   ...    19   59%   58.8    86    20   18    5    0   15    6.2  
20   ...    18   64%   51.4    78    28   18   12    0   13    4.3  
21   ...     0    0%   38.0    38    38   21    7    0    1    3.0  
22   ...     0    0%   44.4    55    39    8    0    0    4    3.5  
23   ...     7   33%   47.0    66    18   23    7    1    8    4.6  
24   ...     0    0%   43.0    43    43   12    6    0    1    7.0  
25   ...    11   52%   57.1    79    23    9    2    2   13    4.5  
26   ...    15   52%   53.0    81     4   19    7    0   16    4.1  
27   ...     6   40%   45.7    71    19   10    5    0    6    4.7  
28   ...     6   25%   45.5    76    26   16    2    0   14    4.0  
29   ...     1    8%   38.6    52    14    7    1    1    5    3.6  
..   ...    ..   ...    ...   ...   ...  ...  ...  ...  ...    ...  
161  ...     0    0%   40.7    61    28   14    5    0    2    4.3  
162  ...    20   61%   54.0    76    21   22    5    0   13    5.3  
163  ...    17   57%   51.9    88    11   17    5    0   22    5.8  
164  ...     1   25%   39.7    57    29    7    3    0    2    2.8  
165  ...     0    0%   29.0    29    29    1    1    0    1    1.0  
166  ...     1   33%   43.3    57    30    6    2    0    1    3.5  
167  ...     3   16%   36.2    63     2   20   10    1   11    4.4  
168  ...     9   35%   47.8    74    10   20    5    0   15    5.2  
169  ...     6   50%   48.1    76    11    7    1    0    8    4.2  
170  ...     0    0%   22.3    34    13    8    3    0    2    3.0  
171  ...     1   25%   35.8    61    17    7    4    1    1    8.4  
172  ...     0    0%   42.0    42    42    0    0    0    1   10.1  
173  ...     1   33%   33.7    61    17    7    4    1    0    7.8  
174  ...    16   50%   50.7    78    16   11    2    0   16    4.5  
175  ...     0    0%   46.0    58    27    0    0    1    1    2.7  
176  ...     0    0%   35.5    52    19    8    2    0    1    3.5  
177  ...    23   70%   57.8    88     7   20    7    0   15    4.9  
178  ...    18   64%   55.0    83     7   20    7    0   13    4.9  
179  ...     5  100%   73.6    88    62    0    0    0    2    5.4  
180  ...     1   50%   42.5    52    33    0    0    0    0    2.0  
181  ...     0    0%   51.0    51    51    0    0    0    1    2.0  
182  ...     0    0%   32.0    32    32    0    0    0    1    0.0  
183  ...     0    0%   19.0    19    19    2    0    0    1    3.0  
184  ...     1  100%   49.0    49    49    3    1    0    1    8.0  
185  ...     0    0%   34.3    44    22   11    5    0    2    5.0  
186  ...     0    0%   52.0    62    42    2    2    0    2    1.0  
187  ...     1   20%   34.0    49     7    4    2    0    2    4.1  
188  ...     1   25%   47.5    69    35    3    1    0    3    2.6  
189  ...     0    0%   29.0    31    27    2    0    0    1    7.2  
190  ...    11   38%   42.3    71    16   14    4    0   16    4.9  

[191 rows x 29 columns]
Unnamed: 0              1
Name          Tim Adleman
Age                    29
Tm                    CIN
IP                  122.1
G                      30
GS                     20
Wgs                     5
Lgs                     9
ND                      6
Wchp                    2
Ltuf                    1
Wtm                     8
Ltm                    12
tmW-L%                0.4
Wist                    2
Lsv                     1
CG                      0
SHO                     0
QS                      7
QS%                   35%
GmScA                46.8
Best                   82
Wrst                   25
BQR                     9
BQS                     5
sDR                     0
IDR                     8
RS/GS                 5.1
RS/IP                 4.8
IP/GS                 5.3
Pit/GS                 90
<80                     3
80-99                  13
100-119                 4
≥120                    0
Max                   110
Name: 0, dtype: object
Unnamed: 0                 1
Name          Andrew Albers*
Age                       31
Tm                       SEA
IP                        41
G                          9
GS                         6
Wgs                        4
Lgs                        1
ND                         1
Wchp                       2
Ltuf                       0
Wtm                        5
Ltm                        1
tmW-L%                 0.833
Wist                       0
Lsv                        1
CG                         0
SHO                        0
QS                         2
QS%                      33%
GmScA                   50.3
Best                      70
Wrst                      25
BQR                        7
BQS                        5
sDR                        0
IDR                        5
RS/GS                      6
Name: 0, dtype: object