π§° Setup Guide for ftw-python-ingestion
This guide walks you through installing Python, Git, and uv, then cloning and running the repository
β‘οΈ Repo link
1οΈβ£ Install Python
πͺ Windows
-
Go to python.org/downloads
-
Download the latest Python 3.11 or 3.12 installer.
-
Check β
βAdd Python to PATHβ during installation.
-
Verify installation:
π§ Linux (Ubuntu / Debian)
sudo apt update
sudo apt install -y python3 python3-venv python3-pip
python3 --version
π macOS
Install using Homebrew:
brew install python
python3 --version
2οΈβ£ Install Git
Git is required to clone the repository.
Windows
Linux
sudo apt install git -y
git --version
macOS
brew install git
git --version
3οΈβ£ Install uv (Python project manager)
π Official instructions: docs.astral.sh/uv/getting-started/installation/
Universal one-liner
curl -LsSf https://astral.sh/uv/install.sh | sh
After installation, verify:
If not found, restart your terminal or add ~/.local/bin to your PATH.
4οΈβ£ Clone the repository
git clone https://github.com/ogbinar/ftw-python-ingestion.git
cd ftw-python-ingestion
5οΈβ£ Create and activate a virtual environment
uv venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
6οΈβ£ Install dependencies
uv pip install -r requirements.txt
If you plan to use Playwright (for 05-webscrape-pw.py):
uv pip install playwright
uv run playwright install chromium
uv run playwright install deps
7οΈβ£ Run the example scripts
Each script shows a different data ingestion method.
python 01-flat-files.py # Read CSV, TSV, JSON, XML, YAML, HTML
python 02-database.py # Query SQLite (Chinook DB)
python 03-api.py # Fetch data from a JSON API
python 04-webscrape-bs.py # Scrape with BeautifulSoup
python 05-webscrape-api.py # Scrape hidden JSON API
python 05-webscrape-pw.py # Scrape Lazada (Playwright)
All output files will appear in the output/ folder.
8οΈβ£ Optional: folder prep
Place your inputs (like chinook.db or sample CSVs) in the data/ folder.
π§ Data Ingestion in Python β Mini Lecture and Guide
This repository demonstrates five fundamental ways to bring data into Python, from simple flat files to dynamic web scraping.
Each script introduces a new data ingestion concept and a set of important libraries for data engineers and analysts.
ποΈ 01 β Flat Files (01-flat-files.py)
π‘ Concept
Reading structured and semi-structured files (CSV, TSV, JSON, XML, YAML, HTML) into pandas DataFrames β the foundation of most data work.
π§° Key Libraries
- pandas β handles tabular data, file I/O, and transformations.
- yaml β parses YAML configuration-style data.
π§ Core Idea
Data often comes in different formats β pandas can unify them all into a consistent DataFrame structure.
π Example
pd.read_csv("data/test.csv")
pd.read_json("data/test.json")
pd.read_xml("data/test.xml")
pd.read_html("data/test.html")[0]
π¦ 02 β Database Access (02-database.py)
π‘ Concept
Querying a relational database directly from Python, using Ibis to abstract SQL.
π§° Key Libraries
- ibis β a high-level data query framework that works across multiple backends (SQLite, Postgres, DuckDB, etc.).
- pandas β to collect query results and export to CSV.
π§ Core Idea
Instead of writing SQL manually, you can express queries with Python syntax β portable across databases.
π Example
con = ibis.sqlite.connect("data/chinook.db")
invoices = con.table("invoices")
customers = con.table("customers")
joined = invoices.join(customers, "CustomerId")
result = joined.group_by("Country").aggregate(avg_invoice=invoices.Total.mean())
βοΈ 03 β API Data (03-api.py)
π‘ Concept
Accessing remote APIs that serve JSON data over HTTP β a common pattern for weather, finance, or social media data.
π§° Key Libraries
- requests β makes HTTP requests easily.
- pandas β normalizes and structures JSON responses.
π§ Core Idea
APIs provide machine-readable data. You can treat them as live data sources for analytics pipelines.
π Example
response = requests.get("https://api.open-meteo.com/v1/forecast", params={"latitude": 14.6, "longitude": 121.0})
data = response.json()
df = pd.json_normalize(data)
π 04 β Web Scraping (HTML) (04-webscrape-bs.py)
π‘ Concept
Extracting structured information from web pages using HTML parsing.
π§° Key Libraries
- requests β fetches the HTML content.
- BeautifulSoup (bs4) β parses and navigates the HTML DOM.
- pandas β stores the extracted table data.
π§ Core Idea
When data isnβt available via API, you can extract it from a siteβs HTML structure β carefully and ethically.
π Example
soup = BeautifulSoup(requests.get(url).text, "html.parser")
table = soup.find("table", {"class": "wikitable"})
rows = [[td.get_text(strip=True) for td in tr.find_all("td")] for tr in table.find_all("tr")]
df = pd.DataFrame(rows)
βοΈ 05 β Web API Scraping (Hidden JSON) (05-webscrape-api.py)
π‘ Concept
Some websites fetch data dynamically from hidden JSON endpoints β the same APIs the browser uses.
π§° Key Libraries
- requests β calls the hidden API directly.
- pandas β converts JSON into CSV-ready format.
π§ Core Idea
Instead of parsing HTML, you can often find the underlying API and access clean, structured JSON data.
π Example
r = requests.get("https://quotes.toscrape.com/api/quotes?page=1")
data = r.json()
df = pd.DataFrame([{"text": q["text"], "author": q["author"]["name"]} for q in data["quotes"]])
π€ 06 β Dynamic Web Scraping with Playwright (05-webscrape-pw.py)
π‘ Concept
Handling JavaScript-rendered sites where HTML content loads dynamically after page load.
π§° Key Libraries
- Playwright β automates browsers (Chromium, Firefox, WebKit).
- pandas β structures the collected data.
- re β cleans and extracts specific text patterns.
π§ Core Idea
When traditional scraping fails (because of JavaScript), use a headless browser to load and interact with the page like a human.
π Example
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://www.lazada.com.ph/")
page.wait_for_selector(".card-jfy-item-desc")
# extract product info from visible cards...
π Key Takeaways
| Technique |
Data Source |
Toolset |
Concept |
| Flat Files |
Local files |
pandas, yaml |
DataFrames from files |
| Database |
SQL / SQLite |
ibis |
Pythonic SQL |
| Public API |
REST JSON |
requests, pandas |
Remote data ingestion |
| Web HTML |
Web pages |
requests, BeautifulSoup |
HTML scraping |
| Hidden API |
Dynamic sites |
requests |
JSON endpoint scraping |
| Browser Scraping |
JS-driven pages |
Playwright |
Headless automation |
π¬ Instructor Notes
- Start with pandas for flat file reading β itβs the βhello worldβ of data ingestion.
- Move to Ibis to understand data abstraction and query portability.
- Explore requests + BeautifulSoup to grasp HTTP and HTML parsing.
- Use Playwright for advanced, real-world scraping tasks.
Together, these form the core ingestion toolkit for any data engineer or data scientist.