DEV Community

IntelliTools
IntelliTools

Posted on

How to Filter Reddit Jobs by Upvotes with Python

Tracking relevant job postings on Reddit can be a time-consuming task if you're doing it manually. The Reddit platform is rich with job listings, but sifting through thousands of posts to find the most upvoted and relevant ones is not scalable. In this article, I'll show you how to automate this process using Python, and how to filter by upvotes and relevance — a technique that can save you hours weekly and help you focus on what matters.

Reddit's API allows access to job postings, but parsing the data manually is inefficient. The key is to leverage Python's ability to scrape and filter data programmatically. By using the requests and BeautifulSoup libraries, we can fetch job data from Reddit and process it to extract only the most relevant entries.

Let's start by writing a script that fetches job data from a specific subreddit. The script will use the requests library to make a GET request to Reddit's API and then use BeautifulSoup to parse the HTML content.

import requests
from bs4 import BeautifulSoup

def fetch_reddit_jobs(subreddit='learnpython'):
    url = f'https://www.reddit.com/r/{subreddit}.json'
    headers = {'User-Agent': 'Mozilla/5.0'}
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    posts = soup.find_all('div', {'class': 'entry'})
    jobs = []
    for post in posts:
        title = post.find('h3').text.strip()
        upvotes = post.find('div', {'class': 'score unvoted'}).text.strip()
        jobs.append({'title': title, 'upvotes': upvotes})
    return jobs
Enter fullscreen mode Exit fullscreen mode

This script fetches job data from the learnpython subreddit and extracts the title and upvote count for each post. You can modify the subreddit parameter to target different communities, such as r/python or r/jobs.

Once we have the job data, the next step is to filter by upvotes. We can define a threshold and only include jobs that meet or exceed it. This ensures we're focusing on the most relevant and popular listings.

def filter_jobs_by_upvotes(jobs, min_upvotes=1000):
    filtered_jobs = [job for job in jobs if int(job['upvotes']) >= min_upvotes]
    return filtered_jobs
Enter fullscreen mode Exit fullscreen mode

This function filters out any job that has fewer than 1000 upvotes. You can adjust the min_upvotes parameter to suit your needs. The result is a list of job postings that are both relevant and highly upvoted.

After filtering, we can export the data to a CSV file for easy analysis. This is where the pandas library comes in handy. It allows us to convert the filtered job data into a DataFrame and then export it to a CSV file.

import pandas as pd

def export_jobs(jobs, filename='reddit_jobs.csv'):
    df = pd.DataFrame(jobs)
    df.to_csv(filename, index=False)
    print(f"Exported {len(jobs)} jobs to {filename}")
Enter fullscreen mode Exit fullscreen mode

This function takes the filtered job data and exports it to a CSV file. You can customize the filename and the columns to include based on your specific requirements.

Putting it all together, we can create a main function that orchestrates the fetching, filtering, and exporting of job data.

def main():
    jobs = fetch_reddit_jobs(subreddit='learnpython')
    filtered_jobs = filter_jobs_by_upvotes(jobs, min_upvotes=1000)
    export_jobs(filtered_jobs)

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

This script automates the entire process of fetching, filtering, and exporting job data from Reddit. It's a powerful tool for developers looking to track relevant job postings without the manual effort.

For developers who want to streamline this process further, the Reddit Job Exporter tool is a great option. It provides a ready-to-run script with all the necessary functionality, including upvote filtering and CSV export. You can find it at https://intellitools.gumroad.com/l/reddit-job-exporter. This tool is designed to save you hours weekly by automating the job tracking process, making it an essential addition to your development workflow.

Top comments (0)