Skip to content

Instantly share code, notes, and snippets.

@leimao
Created January 2, 2025 17:17
Show Gist options
  • Save leimao/5c5cffc01f0db8b4334ace3267ddc851 to your computer and use it in GitHub Desktop.
Save leimao/5c5cffc01f0db8b4334ace3267ddc851 to your computer and use it in GitHub Desktop.
GPU Compute Capability Scraper
from bs4 import BeautifulSoup
import requests
def get_gpu_info(url):
"""Parses the given URL and extracts GPU, compute capability, and category information."""
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
all_gpu_info = []
current_category = None
for element in soup.find_all(['h3', 'table']):
if element.name == 'h3':
current_category = element.text.strip()
elif element.name == 'table':
headers = [th.text.strip() for th in element.find_all('th')]
if 'GPU' in headers and 'Compute Capability' in headers:
gpu_info = []
for row in element.find_all('tr')[1:]:
data = [td.text.strip() for td in row.find_all('td')]
if len(data) == len(headers):
gpu_data = dict(zip(headers, data))
gpu_data['Category'] = current_category
gpu_info.append(gpu_data)
all_gpu_info.extend(gpu_info)
return all_gpu_info
except requests.exceptions.RequestException as e:
print(f"Error fetching URL: {e}")
return []
except Exception as e:
print(f"An error occurred: {e}")
return []
def sort_gpus(gpu_list):
"""Sorts the GPU list by compute capability (descending)."""
def compute_capability_key(gpu):
cc = gpu.get('Compute Capability', '0.0') # Default to 0.0 if not found
try:
return float(cc) #Convert to float for numerical sorting
except ValueError: # Handle cases like "5.0+"
try:
return float(cc[:-1]) # Try removing the last character (+)
except ValueError:
return 0.0 # Default to 0 if neither works
return sorted(gpu_list, key=compute_capability_key, reverse=True)
def print_markdown(gpu_list):
"""Prints the GPU information in Markdown format."""
if not gpu_list:
print("No GPU information found.")
return
print("| GPU | Category | Compute Capability |")
print("|:---:|:---:|:---:|") # Markdown table header separator
for gpu in gpu_list:
gpu_name = gpu.get('GPU', 'N/A').replace("|", "\\|") # Escape pipes in GPU names
category = gpu.get('Category', 'N/A').replace("|", "\\|") # Escape pipes in category names
cc = gpu.get('Compute Capability', 'N/A').replace("|", "\\|") # Escape pipes in CC
print(f"| {gpu_name} | {category} | {cc} |")
# Get GPU information from the webpage
gpu_info = get_gpu_info('https://developer.nvidia.com/cuda-gpus')
# Sort the GPU information
sorted_gpu_info = sort_gpus(gpu_info)
# Print the sorted information in Markdown format
print_markdown(sorted_gpu_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment