From cad7e179c323619a8de1dcaa4c53458d44147516 Mon Sep 17 00:00:00 2001 From: nprasad2077 <114438461+nprasad2077@users.noreply.github.com> Date: Wed, 10 May 2023 00:52:27 -0500 Subject: [PATCH] db scrape works! --- nba_data/nba_scraper_shot_chart.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/nba_data/nba_scraper_shot_chart.py b/nba_data/nba_scraper_shot_chart.py index 4ea8751..2aab8a7 100644 --- a/nba_data/nba_scraper_shot_chart.py +++ b/nba_data/nba_scraper_shot_chart.py @@ -13,7 +13,8 @@ django.setup() from nba_data.models import PlayerShotChartData player_name = 'LeBron James' -season = 2016 +season = 2022 +# team = 'LAL' # You might want to find a way to get this from the webpage url = 'https://www.basketball-reference.com/players/j/jamesle01/shooting/' + str(season) response = requests.get(url) @@ -28,8 +29,6 @@ shot_chart = shot_chart_soup.find("div", {"id": "shot-wrapper"}) shots = shot_chart.find_all("div", {"class": ["tooltip make", "tooltip miss"]}) -team = 'CLE' # You might want to find a way to get this from the webpage - # save data to database and CSV shot_chart_data = [] @@ -39,12 +38,13 @@ for shot_div in shots: top_value = int(style_values[0].split(':')[1].replace('px', '').strip()) left_value = int(style_values[1].split(':')[1].replace('px', '').strip()) - # Parse tip attribute + # Parse tip attribute tip_parts = shot_div['tip'].split('
') # Within your loop: date_and_opponent = re.split(' at | vs ', tip_parts[0]) date = date_and_opponent[0].strip().split(',')[0] + ',' + date_and_opponent[0].strip().split(',')[1] + team = date_and_opponent[0].strip().split(',')[2].strip() opponent = date_and_opponent[1].strip() quarter_and_time = tip_parts[1].split(',') @@ -92,5 +92,11 @@ with open(csv_output_path, "w", newline="") as csvfile: writer.writeheader() for row in shot_chart_data: writer.writerow(row) + +# Save the records in the database +PlayerShotChartData.objects.bulk_create( + PlayerShotChartData(**row) for row in shot_chart_data +) + print('success') \ No newline at end of file