-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer_data_scraping.py
More file actions
158 lines (118 loc) · 7.13 KB
/
player_data_scraping.py
File metadata and controls
158 lines (118 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
"""player_data_scraping.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1m0fy5GqiLahB8qON87vBHGFivb4Y7MM8
"""
import json
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
from google.colab import files
#FOR SCRAPING PLAYER NAMES AND BIRTHDAYS
downloads_folder = os.path.expanduser("~/Downloads")
csv_filename = os.path.join(downloads_folder, 'players_data.csv')
base_url = "https://www.nflweather.com/week/"
years = ["2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022"]
players_data = pd.DataFrame(columns=['Name', 'Birthday'])
for letter in years:
url = f"{base_url}{letter}/"
csv_filename = f'players_data_{years}.csv'
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
body = soup.find("body")
if body:
wrap = body.find("div", id="wrap")
if wrap:
content = wrap.find("div", id="content")
if content:
section_wrapper = content.find("div", id="all_players")
if section_wrapper:
section_content = section_wrapper.find("div", id="div_players")
if section_content:
player_links = section_content.find_all("a")
player_count = 0
for player_link in player_links:
player_name = player_link.get_text(strip=True)
player_url = player_link['href']
full_player_url = f"https://www.pro-football-reference.com{player_url}"
if response.status_code == 200:
response = requests.get(full_player_url)
playersoup = BeautifulSoup(response.content, "html.parser")
script_tag = playersoup.find("script", type="application/ld+json")
if script_tag:
script_content = script_tag.string
player_data = json.loads(script_content)
player_name = player_data.get("name")
player_birthday = player_data.get("birthDate")
print(f"{player_name} {player_birthday}")
# players_data = players_data.append({'Name': player_name, 'Birthday': player_birthday}, ignore_index = True)
new_data = pd.DataFrame({'Name': [player_name], 'Birthday': [player_birthday]})
players_data = pd.concat([players_data, new_data], ignore_index=True)
time.sleep(3)
else:
print(
f"Failed to retrieve data from {player_url}. Status code: {response.status_code}")
players_data.to_csv(csv_filename, index=False)
print(f"CSV file {csv_filename} saved to this projects folder in PycharmProjects.")
#FOR SCRAPING WEATHER FOR EACH GAME
base_url = "https://www.nflweather.com/week/"
years = ["2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"]
weather_data = pd.DataFrame(columns=['Season', 'Week', 'Team1', 'Team2', 'Weather'])
csv_filename = f'weather_data.csv'
for year in years:
num_weeks = 18 if int(year) >= 2021 else 17
for i in range(1, num_weeks + 1):
url = f"{base_url}{year}/week-{i}"
print(i)
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
body = soup.find("body")
if body:
container = body.find("div", class_="container-game-box")
if container:
divs_with_class = container.find_all("div", class_="game-box w-100 d-flex flex-column flex-lg-row align-items-center shadow-box rounded my-2 py-1")
for div in divs_with_class:
team1name = None
team2name = None
weather = None
teambox = div.find("div", class_="col-12 col-lg-3 text-center flex-centered flex-column flex-lg-row")
if teambox:
nextteambox = teambox.find("div", class_="d-flex justify-content-center align-items-center col-lg-12 px-3")
if nextteambox:
againteambox = nextteambox.find("div", class_="team-game-box justify-content-lg-end")
if againteambox:
next = againteambox.find("div", class_="flex-centered flex-column-reverse flex-xxl-row flex-grow-1")
if next:
next2 = next.find("div", class_="flex-centered flex-column me-1 ms-xxl-auto")
if next2:
team1span = next2.find("span")
if team1span:
team1name = team1span.get_text(strip=True)
print("Team1:", team1name)
team2mainbox = nextteambox.find(lambda tag: tag.name == "div" and "team-game-box" in tag.get("class", []) and "justify-content-lg-end" not in tag.get("class", []))
if team2mainbox:
next = team2mainbox.find("div", class_="flex-centered flex-column flex-xxl-row flex-grow-1")
if next:
next2 = next.find("div", class_="flex-centered flex-column me-xxl-auto")
if next2:
team2span = next2.find("span")
if team2span:
team2name = team2span.get_text(strip=True)
print("Team2:", team2name)
weathermainbox = div.find("div", class_="col-12 col-lg-7 flex-centered info-game-box pe-3 rounded-end")
if weathermainbox:
next = weathermainbox.find("div", class_="row flex-centered py-3 py-lg-2 flex-column flex-sm-row game-info w-100")
if next:
next_divs = next.find_all("div", class_="mx-2")
if len(next_divs) >= 2:
second_mx_2_div = next_divs[1]
weatherspan = second_mx_2_div.find("span")
if weatherspan:
weather = weatherspan.get_text(strip=True)
print("Weather:", weather)
if team1name is not None and team2name is not None:
weather_data.loc[len(weather_data)] = [year, i, team1name, team2name, weather]
# Save to CSV after the loop
weather_data.to_csv(csv_filename, index=False)