import requests
from bs4 import BeautifulSoup
import pandas as pd
base_url = "https://www9.dot.ny.gov/CSSWebView/CSS/Reports/"
list_url = "https://www9.dot.ny.gov/CSSWebView/CSS/Reports/SelectActiveRep.do"
Get list page
session = requests.Session()
response = session.get(list_url)
soup = BeautifulSoup(response.text, 'html.parser')
Find all job report links
job_links = soup.find_all('a', href=True)
jobs_data = []
for link in job_links:
if "ActiveProjectDetail" in link['href']:
job_url = base_url + link['href']
job_response = session.get(job_url)
job_soup = BeautifulSoup(job_response.text, 'html.parser')
# Example: extract project title
project_title = job_soup.find('h2').text.strip() if job_soup.find('h2') else "N/A"
# You can refine this part to extract additional data
all_text = job_soup.get_text(separator="\n")
# Example pattern: Extract a known line like location
location = ""
for line in all_text.splitlines():
if "County:" in line:
location = line.split("County:")[1].strip()
break
jobs_data.append({
"Project Title": project_title,
"Location": location,
"URL": job_url
})
Save to Excel
df = pd.DataFrame(jobs_data)
df.to_excel("NYDOT_Jobs.xlsx", index=False)