from bs4 import BeautifulSoup
import pandas as pd
import re
def state_abbreviation(state_name):
"""Converts a full state name to its abbreviation."""
state_map = {
"California": "CA",
"New York": "NY",
"Texas": "TX",
"Florida": "FL",
"Illinois": "IL",
# Add more states as needed
}
return state_map.get(state_name, state_name) # Return original if not found
def extract_job_postings(html_content):
# Parse the HTML
soup = BeautifulSoup(html_content, 'html.parser')
# Initialize an empty list to store job data
all_jobs = []
# Find all section headers and corresponding job tables
sections = soup.find_all('h3', class_='h2')
for section in sections:
# Get the section name
section_name = section.text.strip()
# Find the next table after the section header
job_table = section.find_next('table', class_='jv-job-list')
if job_table:
# Find all job rows in the table
job_rows = job_table.find_all('tr')[1:] # Skip the header row
for row in job_rows:
# Extract job title and location
job_title_cell = row.find('td', class_='jv-job-list-name')
location_cell = row.find('td', class_='jv-job-list-location')
if job_title_cell and location_cell:
job_title = job_title_cell.text.strip()
job_link = job_title_cell.find('a')['href'] if job_title_cell.find('a') else ''
# Clean up location formatting
location = location_cell.text.strip()
location = ' '.join(location.split()) # Remove extra whitespace and newlines
# Reformat to "City, ST"
match = re.match(r"(.+),\s*(.+)", location)
if match:
city = match.group(1)
state = match.group(2)
state_abbr = state_abbreviation(state)
location = f"{city}, {state_abbr}"
# Append job data to the list
all_jobs.append({
'Section': section_name,
'Job Title': job_title,
'Location': location,
'Job Link': job_link
})
# Convert to DataFrame
return pd.DataFrame(all_jobs)
# Usage
html_content = '''your HTML content here'''
jobs_df = extract_job_postings(html_content)
print(jobs_df)
Key changes:
-
state_abbreviation(state_name) function: This function takes a full state name as input and returns its two-letter abbreviation. It uses a dictionary to map state names to abbreviations. If a state name isn't found in the dictionary, it returns the original state name unchanged. You'll need to expand the state_map dictionary to include all the states you expect to encounter.
-
Regular Expression: The code now uses a regular expression re.match(r"(.+),\s*(.+)", location) to split the location string into city and state parts.
(.+): Matches one or more of any character (except newline) and captures it in group 1 (the city).
,\s*: Matches a comma followed by zero or more whitespace characters.
(.+): Matches one or more of any character (except newline) and captures it in group 2 (the state).
-
State Abbreviation Conversion: The state_abbreviation() function is called to convert the full state name to its abbreviation.
-
Reformatting: The location is then reformatted using an f-string: f"{city}, {state_abbr}".
This revised code will now correctly reformat locations like "San Diego, California" to "San Diego, CA" and handle other cities and states as long as they are included in the state_map dictionary. Remember to add more states to the state_map as needed. If a state isn't in the map, it will remain as the full state name.