-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhat-logos.py
33 lines (26 loc) · 1.97 KB
/
hat-logos.py
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
#This code appears to be a Python script that fetches MLB (Major League Baseball) team logos in SVG format from a specific URL
#and prints the team titles along with their corresponding IDs
import requests
#This line imports the requests module, which allows the script to make HTTP requests.
for x in range(0, 201):
data = requests.get(f"https://www.mlbstatic.com/team-logos/team-cap-on-dark/{x}.svg")
#This loop runs from x = 0 to x = 200 (201 iterations) and attempts to fetch SVG logo data for each MLB team from a URL
#that follows the pattern "https://www.mlbstatic.com/team-logos/team-cap-on-dark/{x}.svg".
#The variable x is used to specify the team ID.
if data.status_code == 200:
#This if statement checks whether the HTTP request was successful. A status code of 200 indicates a successful request.
try:
title = data.text.split("<title>")[1].split("</title>")[0]
print(title, f"({x})")
except:
title = f"unknown_team{x}"
print(title, f"({x})")
#If the HTTP request is successful (status_code == 200), the script attempts to extract the team title from the response data.
#It does this by using string manipulation to find the text between the <title> and </title> tags in the SVG data.
#The team title is then stored in the title variable.
#If the extraction is successful (no exceptions occur), the team title along with its corresponding ID (x) is printed to the console.
#If an exception occurs during the extraction (e.g., the SVG data doesn't contain the expected title structure),
#the script will catch the exception and print a placeholder title ("unknown_team{x}") along with the corresponding ID.
#In summary, this Python script iterates through a range of MLB team IDs and attempts to fetch their SVG logos from a specific URL.
#If successful, it extracts the team titles and prints them along with their respective IDs.
#If an error occurs during extraction, it prints a placeholder title with the team's ID.