DEV Community

Cover image for Lesson 7 – Working with APIs and Web Scraping for HR Automation
Daniel Azevedo
Daniel Azevedo

Posted on

Lesson 7 – Working with APIs and Web Scraping for HR Automation

Welcome back to our Python from 0 to Hero series! So far, we’ve learned how to manipulate data and use powerful external libraries for tasks related to payroll and HR systems. But what if you need to fetch real-time data or interact with external services? That’s where APIs and web scraping come into play.

In this lesson, we will cover:

  1. What APIs are and why they are useful.
  2. How to interact with REST APIs using Python’s requests library.
  3. How to apply web scraping techniques to extract data from websites.
  4. Practical examples, such as fetching real-time tax rates for payroll or scraping employee benefits data from a website.

By the end of this lesson, you will be able to automate external data retrieval, making your HR systems more dynamic and data-driven.


1. What Are APIs?

An API (Application Programming Interface) is a set of rules that allows different software applications to communicate with each other. In simpler terms, it lets you interact with another service or database directly from your code.

For example:

  • You can use an API to fetch real-time tax rates for payroll calculations.
  • You might integrate with an HR software API to pull employee data directly into your system.
  • Or you can use a weather API to know when to offer special benefits to employees based on extreme weather conditions.

Most APIs use a standard called REST (Representational State Transfer), which allows you to send HTTP requests (like GET or POST) to access or update data.


2. Using the Requests Library to Interact with APIs

Python’s requests library makes it easy to work with APIs. You can install it by running:

pip install requests
Enter fullscreen mode Exit fullscreen mode

Making a Basic API Request

Let’s start with a simple example of how to fetch data from an API using a GET request.

import requests

# Example API to get public data
url = "https://jsonplaceholder.typicode.com/users"
response = requests.get(url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
    data = response.json()  # Parse the response as JSON
    print(data)
else:
    print(f"Failed to retrieve data. Status code: {response.status_code}")
Enter fullscreen mode Exit fullscreen mode

In this example:

  • We use the requests.get() function to fetch data from the API.
  • If the request is successful, the data is parsed as JSON, and we can process it.

HR Application Example: Fetching Real-Time Tax Data

Let’s say you want to fetch real-time tax rates for payroll purposes. Many countries provide public APIs for tax rates.

For this example, we’ll simulate fetching data from a tax API. The logic would be similar when using an actual API.

import requests

# Simulated API for tax rates
api_url = "https://api.example.com/tax-rates"
response = requests.get(api_url)

if response.status_code == 200:
    tax_data = response.json()
    federal_tax = tax_data['federal_tax']
    state_tax = tax_data['state_tax']

    print(f"Federal Tax Rate: {federal_tax}%")
    print(f"State Tax Rate: {state_tax}%")

    # Use the tax rates to calculate total tax for an employee's salary
    salary = 5000
    total_tax = salary * (federal_tax + state_tax) / 100
    print(f"Total tax for a salary of ${salary}: ${total_tax:.2f}")
else:
    print(f"Failed to retrieve tax rates. Status code: {response.status_code}")
Enter fullscreen mode Exit fullscreen mode

This script could be adapted to work with a real tax rate API, helping you keep your payroll system up-to-date with the latest tax rates.


3. Web Scraping to Gather Data

While APIs are the preferred method for fetching data, not all websites provide them. In those cases, web scraping can be used to extract data from a webpage.

Python’s BeautifulSoup library, along with requests, makes web scraping easy. You can install it by running:

pip install beautifulsoup4
Enter fullscreen mode Exit fullscreen mode

Example: Scraping Employee Benefit Data from a Website

Imagine you want to scrape data about employee benefits from a company’s HR website. Here’s a basic example:

import requests
from bs4 import BeautifulSoup

# URL of the webpage you want to scrape
url = "https://example.com/employee-benefits"
response = requests.get(url)

# Parse the page content with BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')

# Find and extract the data you need (e.g., benefits list)
benefits = soup.find_all("div", class_="benefit-item")

# Loop through and print out the benefits
for benefit in benefits:
    title = benefit.find("h3").get_text()
    description = benefit.find("p").get_text()
    print(f"Benefit: {title}")
    print(f"Description: {description}\n")
Enter fullscreen mode Exit fullscreen mode

In this example:

  • We request the content of a webpage using requests.get().
  • The BeautifulSoup object parses the HTML content.
  • We then extract the specific elements we’re interested in (e.g., benefits titles and descriptions) using find_all().

This technique is useful for gathering HR-related data like benefits, job postings, or salary benchmarks from the web.


4. Combining APIs and Web Scraping in HR Applications

Let’s put everything together and create a mini-application that combines API usage and web scraping for a real-world HR scenario: calculating the total cost of an employee.

We’ll:

  • Use an API to get real-time tax rates.
  • Scrape a webpage for additional employee benefit costs.

Example: Total Employee Cost Calculator

import requests
from bs4 import BeautifulSoup

# Step 1: Get tax rates from API
def get_tax_rates():
    api_url = "https://api.example.com/tax-rates"
    response = requests.get(api_url)

    if response.status_code == 200:
        tax_data = response.json()
        federal_tax = tax_data['federal_tax']
        state_tax = tax_data['state_tax']
        return federal_tax, state_tax
    else:
        print("Error fetching tax rates.")
        return None, None

# Step 2: Scrape employee benefit costs from a website
def get_benefit_costs():
    url = "https://example.com/employee-benefits"
    response = requests.get(url)

    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        # Let's assume the page lists the monthly benefit cost
        benefit_costs = soup.find("div", class_="benefit-total").get_text()
        return float(benefit_costs.strip("$"))
    else:
        print("Error fetching benefit costs.")
        return 0.0

# Step 3: Calculate total employee cost
def calculate_total_employee_cost(salary):
    federal_tax, state_tax = get_tax_rates()
    benefits_cost = get_benefit_costs()

    if federal_tax is not None and state_tax is not None:
        # Total tax deduction
        total_tax = salary * (federal_tax + state_tax) / 100

        # Total cost = salary + benefits + tax
        total_cost = salary + benefits_cost + total_tax
        return total_cost
    else:
        return None

# Example usage
employee_salary = 5000
total_cost = calculate_total_employee_cost(employee_salary)

if total_cost:
    print(f"Total cost for the employee: ${total_cost:.2f}")
else:
    print("Could not calculate employee cost.")
Enter fullscreen mode Exit fullscreen mode

How It Works:

  1. The get_tax_rates() function retrieves tax rates from an API.
  2. The get_benefit_costs() function scrapes a webpage for the employee benefits cost.
  3. The calculate_total_employee_cost() function calculates the total cost by combining salary, taxes, and benefits.

This is a simplified example but demonstrates how you can combine data from different sources (APIs and web scraping) to create more dynamic and useful HR applications.


Best Practices for Web Scraping

While web scraping is powerful, there are some important best practices to follow:

  1. Respect the website’s robots.txt: Some websites don’t allow scraping, and you should check their robots.txt file before scraping.
  2. Use appropriate intervals between requests: Avoid overloading the server by adding delays between requests using the time.sleep() function.
  3. Avoid scraping sensitive or copyrighted data: Always make sure you’re not violating any legal or ethical rules when scraping data.

Conclusion

In this lesson, we explored how to interact with external services using APIs and how to extract data from websites through web scraping. These techniques open up endless possibilities for integrating external data into your Python applications, especially in an HR context.

Top comments (0)