Step 4: Opening python
I use Spyder as the development environment.
Search for Spyder in your start menu.
Click to open
It will bring it up, with a temp file loaded.
Click new file
Save as "MLB lineup scrape" or a file name of your choosing. Save it where you want your end files to go, something like documents or a folder on your desktop.
Paste the code below
Edit the path to your driver file, it needs to be inside the ' ', single quotes. It does not need an extension, it should just end in chromedriver. There needs to be double slashes \\ in the path. Like:
'C:\\user\\documents\\chromedriver'
and click the green run button.
Code:
import pandas as pd
from selenium import webdriver
driver = webdriver.Chrome('Path to chrome driver')
teams = ['BAL',
'BOS',
'CWS',
'CLE',
'DET',
'HOU',
'KC',
'LAA',
'MIN',
'NYY',
'OAK',
'SEA',
'TB',
'TEX',
'TOR',
'ARI',
'ATL',
'CHC',
'CIN',
'COL',
'LAD',
'MIA',
'MIL',
'NYM',
'PHI',
'PIT',
'SD',
'SF',
'STL',
'WAS']
for x in teams:
driver.get('https://www.rotowire.com/baseball/batting-orders.php?team='+x)
battertables = driver.find_elements_by_xpath('//ol[@class="list is-rankings pad-5-10"]')
rbatteritmes = battertables[1].find_elements_by_xpath('.//li[@class="md-text"]')
rbatters = []
for z in rbatteritmes:
rbatters.append(z.text)
Lbatteritems = battertables[2].find_elements_by_xpath('.//li[@class="md-text"]')
Lbatters = []
for z in Lbatteritems:
Lbatters.append(z.text)
teamvsr = pd.DataFrame({x:rbatters})
teamvsl = pd.DataFrame({x:Lbatters})
if x == 'BAL':
vsRight = teamvsr
else:
vsRight = vsRight.join(teamvsr)
if x == 'BAL':
VsLeft = teamvsl
else:
VsLeft = VsLeft.join(teamvsl)
driver.close()
vsRight.to_csv('VsRightBattingLineups.csv')
VsLeft.to_csv('VsLeftBattingLineups.csv')
The script should pull up chrome, and you should see it going between the team tabs. It should output two CSV files, on with the default vs Right, the other with default vs left.