Skip to content
Advertisement

How to print MLB data into Pandas DataFrame?

I am still learning how to web scrape and could use some help. I would like to print the MLB data into a Pandas DataFrame.

It looks like the program does not run correctly but I did not receive an error. Any suggestions would be greatly appreciated. Thanks in advance for any help that you may offer.

import pandas as pd
import requests

url = 'https://www.baseball-reference.com/data/war_daily_bat.txt'
headers = {'User-Agent':
           'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}

df = pd.read_html(url)

response = requests.get(url, headers=headers)

Advertisement

Answer

That page contains a text file in CSV format. So load it with pandas like this:

    pd.read_csv(url)

And that should get you what you are looking for.

Advertisement