Student Login
Python Programming

APIs and Web Scraping

Introduction

  • Now that you know file handling, let's learn to get data from the internet! This opens up endless automation possibilities.
  • You'll learn to use APIs and extract data from websites.

What is an API?

  • An API (Application Programming Interface) lets your program communicate with other services.
  • Example: Get weather data, currency rates, news articles, or social media posts automatically.
  • Python's `requests` library makes this simple. Install it: `pip install requests`

Making API Requests

  • “`python
  • import requests
  • # Get data from an API
  • response = requests.get('https://api.github.com/users/github')
  • data = response.json() # Convert to Python dictionary
  • print(data['name'])
  • print(data['followers'])
  • “`
  • Real Use Cases:
  • – Get crypto prices automatically
  • – Check weather for your city
  • – Fetch news headlines
  • – Get exchange rates for currency conversion

Web Scraping Basics

  • Web scraping means extracting data from websites. Install BeautifulSoup: `pip install beautifulsoup4`
  • “`python
  • import requests
  • from bs4 import BeautifulSoup
  • url = 'https://example.com'
  • response = requests.get(url)
  • soup = BeautifulSoup(response.content, 'html.parser')
  • # Find all headings
  • headings = soup.find_all('h1')
  • for heading in headings:
  • print(heading.text)
  • “`

Automation Example: Price Monitor

  • “`python
  • import requests
  • import time
  • def check_crypto_price():
  • url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot'
  • response = requests.get(url)
  • data = response.json()
  • price = data['data']['amount']
  • print(f'Bitcoin Price: ${price}')
  • return float(price)
  • # Check price every hour
  • while True:
  • price = check_crypto_price()
  • if price < 40000:
  • print('🔔 Price Alert! Bitcoin below $40,000')
  • time.sleep(3600) # Wait 1 hour
  • “`

Freelance Opportunities

  • Web scraping and API integration are highly demanded skills:
  • Common Projects:
  • – Scrape product prices from e-commerce sites
  • – Extract contact information from business directories
  • – Monitor competitor prices automatically
  • – Collect data for research or analysis
  • Earnings: $30-200 per small scraping project on Fiverr/Upwork
⬅️ Previous Lesson Working with Files Next Lesson ➡️ Building Projects
Scroll to Top