python requests module projects

Python Requests Module Projects

If you're looking to work with APIs or web scraping in Python, then the Requests module is an essential tool to have in your arsenal. This module allows you to send HTTP requests using Python and also handles the response in a more user-friendly way. In this post, I'll be sharing some project ideas that you can create using the Python Requests module.

1. Weather App

The weather app is a great project idea to implement using the Python Requests module. You can use an API like OpenWeatherMap to fetch real-time weather data for any location in the world. You can then parse the JSON response and display the weather information in a user-friendly manner.


import requests

city = input("Enter city name: ")
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY"
response = requests.get(url)

data = response.json()
temp = round(data['main']['temp'] - 273.15, 2)
humidity = data['main']['humidity']
description = data['weather'][0]['description']

print(f"Temperature: {temp}°C")
print(f"Humidity: {humidity}%")
print(f"Description: {description}")
    

2. Image Downloader

Using the Requests module, you can create a program that downloads images from a website. This project can be helpful if you need to download multiple images from a website for any purpose. You can use Beautiful Soup to parse the HTML and then use Requests to download the images.


import requests
from bs4 import BeautifulSoup
import os

url = input("Enter website URL: ")
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
images = soup.find_all('img')

os.mkdir('images')

for img in images:
    try:
        img_url = img['src']
        img_response = requests.get(img_url)

        filename = os.path.join('images', img_url.split('/')[-1])
        with open(filename, 'wb') as f:
            f.write(img_response.content)

    except:
        pass
    

3. Currency Converter

You can also use the Requests module to create a currency converter. You can use an API like Fixer.io to fetch the latest exchange rates and then use the conversion formula to convert between currencies.


import requests

url = "http://data.fixer.io/api/latest?access_key=YOUR_API_KEY"
response = requests.get(url)

data = response.json()
rates = data['rates']

amount = float(input("Enter amount: "))
from_currency = input("Enter from currency: ").upper()
to_currency = input("Enter to currency: ").upper()

converted_amount = amount / rates[from_currency] * rates[to_currency]

print(f"{amount} {from_currency} is equal to {round(converted_amount, 2)} {to_currency}")
    

Conclusion

The Python Requests module is an incredibly powerful tool that can help you work with APIs and web scraping. With these project ideas, you can get started with using the module and create some useful applications.