About Lesson
Write a Script to Scrape Data from a Website
- Objective: Write a Python script that scrapes data from a website using
requests
andBeautifulSoup
. - Steps:
- Send an HTTP request to the website using
requests
. - Parse the HTML content using
BeautifulSoup
. - Extract and print the desired data.
python
import requests from bs4 import BeautifulSoup url = "https://example.com" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") # Extract and print all headings headings = soup.find_all("h1") for heading in headings: print(heading.text)
- Send an HTTP request to the website using
Implement a Program to Fetch and Display Data from an API
- Objective: Write a Python program that fetches data from an API and displays it.
- Steps:
- Send an HTTP request to the API endpoint using
requests
. - Parse the JSON response.
- Display the data in a readable format.
python
import requests api_url = "https://api.example.com/data" response = requests.get(api_url) data = response.json() # Display the data for item in data: print(f"Name: {item['name']}, Age: {item['age']}")
- Send an HTTP request to the API endpoint using