To extract all job postings from the HTML structure provided and organize them into a structured pandas DataFrame, you can use BeautifulSoup for parsing the HTML and pandas for organizing the data. Here's a step-by-step explanation and the corresponding code:
✅ Goal
Extract the following information for each job:
Section (e.g., Accounting, AI & Data Science)
Job Title
Job Link
Job Location
🧠 Approach Overview
Parse the HTML using BeautifulSoup.
Find all sections (<h3> tags with class h2).
For each section, locate the immediately following <table> with class jv-job-list.
Iterate through each <tr> in the <tbody> of that table.
Extract job title and link from <td class="jv-job-list-name">.
Extract location from <td class="jv-job-list-location">, normalizing whitespace.
Store all data in a list of dictionaries, then convert to a DataFrame.
🧾 Python Code
python
User
can you fix this formatting for location ('San Diego,\n California')
Assistant
To fix the formatting issue in the job location—such as strings like 'San Diego,\n California'—we need to ensure that all whitespace is normalized and extra line breaks or indentation are removed.
🛠️ Problem Explanation
The issue arises because the HTML for the job location often contains:
Line breaks (\n)
Indentation (multiple spaces)
Irregular spacing between parts of the location
For example:
html
<td class="jv-job-list-location">
San Diego,
California
</td>
This leads to raw text extraction that includes \n and extra spaces, resulting in strings like:
code
"San Diego,\n California"
✅ Solution: Use get_text(strip=True)
Instead of using .stripped_strings and joining manually, a more robust and concise way is to use:
python
job_location = location_col.get_text(strip=True)
This method:
Strips all leading and trailing whitespace
Collapses any internal whitespace (including \n, \t, and multiple spaces) into a single space
Returns a clean, normalized string like "San Diego, California"
🔁 Updated Code Snippet
Here's the updated section of the code that extracts the job location:
python
# Extract job title, link, and location
job_title = name_col.a.get_text(strip=True) if name_col and name_col.a else ''
job_link = name_col.a['href'] if name_col and name_col.a else ''
job_location = location_col.get_text(strip=True) if location_col else ''
from bs4 import BeautifulSoup
import pandas as pd
# Example HTML content (you can replace this with actual HTML from a request)
html_content = '''
<h3 class="h2">Accounting</h3>
<table class="jv-job-list">
<thead>
<tr>
<th class="jv-cws-sr-only" scope="col">Job listing</th>
<th class="jv-cws-sr-only" scope="col">Job location</th>
</tr>
</thead>
<tbody>
<tr>
<td class="jv-job-list-name">
<a href="/aviditybiosciences/job/oTnIvfwd">Senior AP Specialist I</a>
</td>
<td class="jv-job-list-location">
San Diego,
California
</td>
</tr>
<tr>
<td class="jv-job-list-name">
<a href="/aviditybiosciences/job/oRfUvfwf">Staff Accountant</a>
</td>
<td class="jv-job-list-location">
San Diego,
California
</td>
</tr>
</tbody>
</table>
<h3 class="h2">AI & Data Science</h3>
<table class="jv-job-list">
<thead>
<tr>
<th class="jv-cws-sr-only" scope="col">Job listing</th>
<th class="jv-cws-sr-only" scope="col">Job location</th>
</tr>
</thead>
<tbody>
<tr>
<td class="jv-job-list-name">
<a href="/aviditybiosciences/job/oSbEvfwW">Associate Director, Data Science, R&D (Molecular Epidemiology)</a>
</td>
<td class="jv-job-list-location">
San Diego,
California
</td>
</tr>
</tbody>
</table>
<h3 class="h2">Biology</h3>
<table class="jv-job-list">
<thead>
<tr>
<th class="jv-cws-sr-only" scope="col">Job listing</th>
<th class="jv-cws-sr-only" scope="col">Job location</th>
</tr>
</thead>
<tbody>
<tr>
<td class="jv-job-list-name">
<a href="/aviditybiosciences/job/oXyZ123">Research Scientist - Molecular Biology</a>
</td>
<td class="jv-job-list-location">
Boston, MA
</td>
</tr>
</tbody>
</table>
'''
# Parse the HTML
soup = BeautifulSoup(html_content, 'html.parser')
# List to store job data
job_data = []
# Find all section headers
sections = soup.find_all('h3', class_='h2')
for section in sections:
# Get section name
section_name = section.get_text(strip=True)
# Find the next table with job listings
table = section.find_next_sibling('table', class_='jv-job-list')
if not table:
continue # Skip if no table found
# Iterate over each job row
rows = table.tbody.find_all('tr')
for row in rows:
name_col = row.find('td', class_='jv-job-list-name')
location_col = row.find('td', class_='jv-job-list-location')
job_title = name_col.a.get_text(strip=True) if name_col and name_col.a else ''
job_link = name_col.a['href'] if name_col and name_col.a else ''
job_location = ' '.join(location_col.stripped_strings) if location_col else ''
job_data.append({
'Section': section_name,
'Job Title': job_title,
'Link': job_link,
'Location': job_location
})
# Convert to DataFrame
df = pd.DataFrame(job_data)
# Display the DataFrame
print(df)
📊 Sample Output (DataFrame)
Section
Job Title
Link
Location
Accounting
Senior AP Specialist I
/aviditybiosciences/job/oTnIvfwd
San Diego, California
Accounting
Staff Accountant
/aviditybiosciences/job/oRfUvfwf
San Diego, California
AI & Data Science
Associate Director, Data Science...
/aviditybiosciences/job/oSbEvfwW
San Diego, California
Biology
Research Scientist - Molecular Biology
/aviditybiosciences/job/oXyZ123
Boston, MA
🛠️ Notes
You can enhance the code to handle relative URLs by prepending a base URL if needed.
This code assumes the HTML is well-formed and sections are immediately followed by their job tables.
If more robustness is needed, consider using .find_next() or .find_all_next() with checks to avoid skipping sections.
This approach gives you a clean, structured DataFrame of all job postings grouped by section, ready for further analysis or exporting to CSV/Excel.
job_location = ' '.join(location_col.stripped_strings) if location_col else ''
🧪 Example Output After Fix
Before:
python
"San Diego,\n California"
After:
python
"San Diego, California"
✅ Why This Works Better
Method
Handles Line Breaks
Handles Extra Spaces
Normalizes Whitespace
Simpler
get_text(strip=True)
✅ Yes
✅ Yes
✅ Yes
✅ Yes
' '.join(stripped_strings)
✅ Yes
✅ Yes
✅ Yes
❌ No
Using get_text(strip=True) is cleaner, more readable, and less error-prone, especially when dealing with HTML content that may vary in structure.
📦 Final Note
If you're building or maintaining a web scraping script, always normalize text extraction using .get_text(strip=True) for fields like locations, descriptions, or titles where consistent formatting is essential.
Let me know if you'd like to also clean up job titles or links similarly!