Okay, I will run this Python script for you.
This script is designed to:
- Visit the NYSDOT reports page.
- Find links to individual project detail pages.
- Visit each project detail page.
- Extract the project title and county location from each page.
- Compile this data into a list.
- Save the data into an Excel file named
NYDOT_Jobs.xlsx.
Running the script now...
import requests
from bs4 import BeautifulSoup
import pandas as pd
import os # Import os to confirm file creation
base_url = "https://www9.dot.ny.gov/CSSWebView/CSS/Reports/"
list_url = "https://www9.dot.ny.gov/CSSWebView/CSS/Reports/SelectActiveRep.do"
output_filename = "NYDOT_Jobs.xlsx"
print("Fetching report list page...")
# Get list page
session = requests.Session()
try:
response = session.get(list_url)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
print("Report list page fetched successfully.")
except requests.exceptions.RequestException as e:
print(f"Error fetching report list page: {e}")
exit()
soup = BeautifulSoup(response.text, 'html.parser')
# Find all job report links
print("Finding project detail links...")
job_links = soup.find_all('a', href=True)
jobs_data = []
processed_count = 0
for link in job_links:
if "ActiveProjectDetail" in link['href']:
job_url = base_url + link['href']
processed_count += 1
print(f"Processing project {processed_count}: {job_url}")
try:
job_response = session.get(job_url, timeout=10) # Add timeout
job_response.raise_for_status()
job_soup = BeautifulSoup(job_response.text, 'html.parser')
# Extract project title
project_title = job_soup.find('h2').text.strip() if job_soup.find('h2') else "N/A"
# Extract location (County)
location = "N/A"
all_text = job_soup.get_text(separator="\n")
for line in all_text.splitlines():
if "County:" in line:
parts = line.split("County:")
if len(parts) > 1:
location = parts[1].split("Location:")[0].strip() # Attempt to get just county before Location:
location = location.strip(',') # Remove trailing comma if any
break # Assume first match is correct
jobs_data.append({
"Project Title": project_title,
"County": location,
"URL": job_url
})
except requests.exceptions.RequestException as e:
print(f"Error fetching or parsing {job_url}: {e}")
except Exception as e:
print(f"An unexpected error occurred processing {job_url}: {e}")
print(f"Finished processing {processed_count} potential project links.")
print(f"Found data for {len(jobs_data)} projects.")
# Save to Excel
if jobs_data:
print(f"Saving data to {output_filename}...")
try:
df = pd.DataFrame(jobs_data)
df.to_excel(output_filename, index=False)
print(f"Successfully saved data to {output_filename}")
# Verify file creation
if os.path.exists(output_filename):
print(f"File {output_filename} confirmed to exist.")
else:
print(f"Error: File {output_filename} was not created.")
except Exception as e:
print(f"Error saving data to Excel: {e}")
else:
print("No project data was collected to save.")