I see the user wants a Python program for scraping job listings filtered by keywords. I'll need to think about responsible web scraping, keeping legality and robots.txt in mind. It's important not to suggest violating terms of service. Ideally, I can create a generic code example for sites like Indeed or RemoteOK, using requests and BeautifulSoup. It’ll be essential to filter by keywords in titles or descriptions and possibly output in CSV format.Designing the job scraper
The user asked for code without heavy explanation, but I think some comments are still appropriate. I’m considering hard-coding a specific job board API, like RemoteOK's, since it has an open API to simplify the task and avoid DOM parsing. I’ll implement basic features: accepting keywords, which will be case-insensitive, and using argparse for CLI arguments.
While users might expect HTML scraping, using an API is simpler. I’ll also offer a template function for customizing selectors with BeautifulSoup, reminding them to adjust according to the website's guidelines.Creating a generic HTML snippet
I see the user wants a generic HTML snippet example that uses CSS selectors for job listings. I can suggest selecting elements like .job-card, along with .job-title, .company, and .location, plus the link within the a tag. It’s important to note that users can update the selectors as needed, but I should avoid specific references to sites like "Indeed" due to their anti-scraping policies. Instead, I can remind them to check the terms of service and provide generic parsing approaches without evasion strategies.Designing the job scraping script
I’m putting together a script that fetches job listings using requests and BeautifulSoup. The fetch_jobs function will retrieve and parse job cards from a specified URL, extracting information like the job title, company, location, and URL.
I also have a filtering function to include or exclude jobs based on keywords. It checks each job's title and summary for the keywords provided. In the main section, I’m setting up argparse for command-line inputs, allowing for flexible output options, including CSV files or printed results. Expanding the fields for storage can be considered later.Simplifying the job scraper script
The user didn't specifically request a CLI, so I can simplify the script to just include constants for URL and KEYWORDS and print outputs directly. However, I'll aim to provide a script that’s easy to adapt and understand, partly because they may appreciate flexibility.
I’ll include necessary comments in the code and mention dependencies like requests and beautifulsoup4. I should also note how to customize CSS selectors without discussing circumvention of restrictions. It’s crucial to remind users to ensure the site allows scraping and not to overload servers. I'll prepare a complete script that includes the main execution section for clarity. Below is a complete, minimal example of a Python script that:
Scrapes a job listing page (HTML)
Extracts basic job data (title, company, location, link)
Filters the results by include/exclude keywords
Prints the filtered jobs (or saves to CSV)
You’ll need to adapt the CSS selectors to match the specific job site you’re targeting.
Keep only jobs where title/summary contains “python” or “developer”
Remove jobs that mention “senior” or “manager”
Save results to jobs.csv
Adapting to a real site
Open the site in your browser.
Use “Inspect Element” to find:
The HTML element for each job card (e.g., div.job-search-card)
The title, company, location, and link elements inside each card.
Replace the selectors in fetch_jobs():
python
for card in soup.select(".job-search-card"):
title_el = card.select_one(".job-title-class")
company_el = card.select_one(".company-name-class")
location_el = card.select_one(".location-class")
If you tell me which job board you want to target, I can tailor the selectors and example specifically for that site (as long as it’s allowed by their terms of service).
import argparse
import csv
from urllib.parse import urljoin
import requests
from bs4 import BeautifulSoup
def fetch_jobs(url):
"""
Fetch and parse job listings from a page.
NOTE: You MUST customize the CSS selectors (.job-card, .job-title, etc.)
to match your target website's structure.
"""
headers = {
# A simple user-agent; some sites return different content for non-browser agents
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/124.0 Safari/537.36"
}
resp = requests.get(url, headers=headers, timeout=15)
resp.raise_for_status()
soup = BeautifulSoup(resp.text, "html.parser")
jobs = []
# Change this selector to match the container for a single job posting
for card in soup.select(".job-card"):
title_el = card.select_one(".job-title")
company_el = card.select_one(".company")
location_el = card.select_one(".location")
link_el = card.select_one("a")
title = title_el.get_text(strip=True) if title_el else ""
company = company_el.get_text(strip=True) if company_el else ""
location = location_el.get_text(strip=True) if location_el else ""
url_rel = link_el["href"] if link_el and link_el.has_attr("href") else ""
job_url = urljoin(url, url_rel)
# Use all text inside the card as "description/summary"
summary = card.get_text(" ", strip=True)
jobs.append({
"title": title,
"company": company,
"location": location,
"url": job_url,
"summary": summary,
})
return jobs
def filter_jobs(jobs, include=None, exclude=None):
"""
Filter job dicts by keywords (title + summary).
include: list of keywords that must appear (OR logic; at least one)
exclude: list of keywords that must NOT appear
"""
include = [k.lower() for k in (include or [])]
exclude = [k.lower() for k in (exclude or [])]
filtered = []
for job in jobs:
text = (job.get("title", "") + " " + job.get("summary", "")).lower()
if include and not any(kw in text for kw in include):
continue
if exclude and any(kw in text for kw in exclude):
continue
filtered.append(job)
return filtered
def save_to_csv(jobs, filename):
fieldnames = ["title", "company", "location", "url"]
with open(filename, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for job in jobs:
writer.writerow({
"title": job.get("title", ""),
"company": job.get("company", ""),
"location": job.get("location", ""),
"url": job.get("url", ""),
})
def main():
parser = argparse.ArgumentParser(
description="Scrape job listings from a page and filter by keywords."
)
parser.add_argument("url", help="URL of the job listings page to scrape")
parser.add_argument(
"-k", "--keywords", nargs="+",
help="Include keywords (at least one must match)"
)
parser.add_argument(
"-x", "--exclude", nargs="+",
help="Exclude keywords (none may match)"
)
parser.add_argument(
"-o", "--output",
help="Output CSV file (if omitted, results are printed)"
)
args = parser.parse_args()
# 1) Scrape jobs
jobs = fetch_jobs(args.url)
# 2) Filter by keywords
filtered = filter_jobs(jobs, include=args.keywords, exclude=args.exclude)
# 3) Output
if args.output:
save_to_csv(filtered, args.output)
print(f"Saved {len(filtered)} jobs to {args.output}")
else:
print(f"Found {len(filtered)} matching jobs:\n")
for job in filtered:
print(f"{job['title']} — {job['company']} ({job['location']})")
print(job["url"])
print("-" * 80)
if __name__ == "__main__":
main()