Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- import re
- import time
- game_urls = [
- "https://www.espn.com/mlb/boxscore/_/gameId/401569423",
- ]
- players_to_track = [
- {"name": "M. Betts", "stat": "hits", "threshold": 1},
- {"name": "A. Volpe", "stat": "hits", "threshold": 1},
- {"name": "A. Judge", "stat": "hits", "threshold": 1},
- ]
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
- }
- def get_player_stats(player_info, html_content):
- player_name = player_info['name']
- stat_to_track = player_info['stat']
- threshold = player_info['threshold']
- # Construct regular expression pattern to match player stats
- pattern = r'<div class="full-name">.*?' + re.escape(player_name) + r'.*?<div class="info">(.*?)</div>'
- # Find all instances of player stats
- player_stats = re.findall(pattern, html_content, re.DOTALL)
- for stat in player_stats:
- # Extract hits using regular expression
- hits_match = re.search(r'<span class="number">(\d+)</span>', stat)
- if hits_match:
- hits = int(hits_match.group(1))
- if stat_to_track == "hits":
- return hits
- return 0
- def print_player_performance(player_name, stat_to_track, stat_value, threshold):
- if stat_value >= threshold:
- print("\033[92m" + f"{player_name}: {stat_to_track.replace('_', ' ').capitalize()} - {stat_value}" + "\033[0m")
- else:
- print("\033[91m" + f"{player_name}: {stat_to_track.replace('_', ' ').capitalize()} - {stat_value}" + "\033[0m")
- def get_game_stats(game_urls, players_to_track):
- while True:
- for game_url in game_urls:
- try:
- response = requests.get(game_url, headers=headers)
- response.raise_for_status() # Raise an exception for bad status codes
- html_content = response.text
- for player_info in players_to_track:
- hits_home_runs = get_player_stats(player_info, html_content)
- print_player_performance(player_info['name'], player_info['stat'], hits_home_runs, player_info['threshold'])
- except requests.exceptions.RequestException as e:
- print("Failed to retrieve game stats from", game_url, ". Error:", e)
- except Exception as e:
- print("An error occurred while processing", game_url, ". Error:", e)
- time.sleep(60)
- get_game_stats(game_urls, players_to_track)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement