[Python] Web Scraping ESPN NFL webpage with Python

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to perform web scraping using Python on the ESPN website to extract historical NFL football game results scores only into a csv file. I’m unable to find a way to add the dates as displayed in the desired output. Could someone help me a way to get the desired output from the current output. The website I am using to scrape the data and the desired output is below:

    NFL Website: https://www.espn.com/nfl/scoreboard/_/week/17/year/2022/seasontype/2

    Current Output:

    Week #, Away Team, Away Score, Home Team, Home Score
    Week 17, Cowboys, 27, Titans, 13
    Week 17, Cardinals, 19, Falcons, 20
    Week 17, Bears, 10, Lions, 41


    Desired Game Results Output:

    Week #, Date, Away Team, Away Score, Home Team, Home Score
    Week 17, 12/29/2022, Cowboys, 27, Titans, 13
    Week 17, 1/1/2023, Cardinals, 19, Falcons, 20
    Week 17, 1/1/2023, Bears, 10, Lions, 41


    Code:

    import bs4
    from bs4 import BeautifulSoup
    import requests
    import pandas as pd
    import numpy as np

    daterange = 1
    url_list = []

    while daterange < 19:
    url = "https://www.espn.com/nfl/scoreboard/_/week/"+str(daterange)+"/year/2022/seasontype/2"
    url_list.append(url)
    daterange = daterange + 1


    j = 1
    away_team = []
    home_team = []
    away_team_score = []
    home_team_score = []
    week = []

    for url in url_list:
    response = urlopen(url)
    urlname = requests.get(url)
    bs = bs4.BeautifulSoup(urlname.text,'lxml')
    print(response.url)
    i = 0
    while True:
    try:
    name = bs.findAll('div',{'class':'ScoreCell__TeamName ScoreCell__TeamName--shortDisplayName truncate db'})
    except Exception:
    break

    name = name.get_text()
    try:
    score = bs.findAll('div',{'class':'ScoreCell__Score h4 clr-gray-01 fw-heavy tar ScoreCell_Score--scoreboard pl2'})
    except Exception:
    break


    score = score.get_text()

    if i%2 == 0:
    away_team.append(name)
    away_team_score.append(score)

    else:
    home_team.append(name)
    home_team_score.append(score)
    week.append("week "+str(j))

    i = i + 1
    j = j + 1

    web_scraping = list (zip(week, home_team, home_team_score, away_team, away_team_score))
    web_scraping_df = pd.DataFrame(web_scraping, columns = ['week','home_team','home_team_score','away_team','away_team_score'])
    web_scraping_df

    Continue reading...

Compartilhe esta Página