Surya_from_1627

WSP3

Sep 7th, 2026 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3.  
  4. def scrape_dataset_count():
  5.     url = "https://catalog.data.gov/dataset"
  6.    
  7.     # Header needed to mimic a standard browser request
  8.     headers = {
  9.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
  10.     }
  11.  
  12.     try:
  13.         response = requests.get(url, headers=headers, timeout=10)
  14.        
  15.         if response.status_code == 200:
  16.             soup = BeautifulSoup(response.text, "html.parser")
  17.            
  18.             # Target the specific span class highlighted in your browser inspection
  19.             count_span = soup.find("span", class_="text-heavy text-secondary font-sans-lg")
  20.            
  21.             if count_span:
  22.                 # Extracts the raw text (e.g., "557,486")
  23.                 count = count_span.text.strip()
  24.                 print(f"Number of datasets available on Data.gov: {count}")
  25.             else:
  26.                 print("Could not locate the dataset count element on the page.")
  27.         else:
  28.             print(f"failed to retrieve webpage. Status code: {response.status_code}")
  29.  
  30.     except requests.exceptions.RequestException as e:
  31.         print(f"Error fetching Data.gov webpage: {e}")
  32.  
  33.  scrape_dataset_count()
Advertisement
Add Comment
Please, Sign In to add comment