PTENES
Skip to content
MODULE 5.2

⚡ Script Automation

Web scraping, bulk file processing, data transformation, and scheduling with cron. Scripts that work while you sleep.

6
Topics
~45
Minutes
5.1
Prerequisite
Practical
Type
Progress: 0% 0 of 6
1

🌐 Web Scraping with requests+bs4

The combo requests + BeautifulSoup is the Python standard for extracting data from web pages. Ask Claude Code: "scrape os titulos e precos desta pagina" and it generates the complete script with CSS selectors, error handling, and rate limiting.

scraper.py
import requests
from bs4 import BeautifulSoup
import time

def scrape_titles(url: str) -> list[str]:
    resp = requests.get(url, timeout=10)
    resp.raise_for_status()
    soup = BeautifulSoup(resp.text, "html.parser")
    return [h.text.strip() for h in soup.select("h2.title")]

urls = ["https://example.com/page/1", "https://example.com/page/2"]
for url in urls:
    print(scrape_titles(url))
    time.sleep(1)  # rate limiting

In one sentence: requests + BeautifulSoup = web data in just a few lines.

2

📁 Bulk File Processing

Rename 500 files, convert formats in batches, extract metadata. With pathlib e shutil, Python processes hundreds of files in the time it would take you to do 5 manually. Claude Code generates these scripts on demand.

batch_rename.py
from pathlib import Path

src = Path("./downloads")
for i, f in enumerate(sorted(src.glob("*.jpg")), 1):
    new_name = f"photo_{i:04d}{f.suffix}"
    f.rename(f.parent / new_name)
    print(f"  {f.name} -> {new_name}")

In one sentence: pathlib + glob = process hundreds of files in a loop.

3

🔄 Data Transforms (CSV/JSON)

Convert CSV to JSON, filter rows, aggregate data. The modules csv e json from the stdlib handle 90% of cases. For more complex transformations, pandas is the Swiss Army knife.

In one sentence: csv.DictReader + json.dump = pure-Python data pipeline.

4

⏰ Scheduling with cron

Real automation means running without intervention. The cron (Linux/macOS) schedules scripts to run at set times. For more complex tasks, systemd timers offer automatic logging and retries.

crontab
# Rodar todo dia as 6h
0 6 * * * /home/user/.venv/bin/python /home/user/scripts/scrape.py >> /tmp/scrape.log 2>&1

# Rodar a cada 30 minutos
*/30 * * * * /home/user/.venv/bin/python /home/user/scripts/check.py

In one sentence: cron = your script running on its own at the right time, every day.

5

🔌 Third-Party APIs

Powerful scripts connect services: GitHub API for repos, Slack for notifications, Stripe for payments. With requests and authentication headers, you can integrate any REST API. Claude Code knows the endpoints of popular APIs and generates the integration code.

In one sentence: requests + auth headers = integration with any service.

6

🛡️ Robust Error Handling

Production scripts need try/except well thought out, logging structured and retries for transient failures. The library tenacity simplifies retries with exponential backoff. Ask Claude Code: "adicione error handling robusto a este script".

robust_script.py
import logging
from tenacity import retry, stop_after_attempt, wait_exponential

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)

@retry(stop=stop_after_attempt(3), wait=wait_exponential(min=1, max=10))
def fetch_data(url: str) -> dict:
    log.info(f"Fetching {url}")
    resp = requests.get(url, timeout=10)
    resp.raise_for_status()
    return resp.json()

In one sentence: try/except + logging + retries = scripts that don't fail silently.

🧾 Module Summary

✓
Web scraping with requests + BeautifulSoup.
✓
File processing in bulk with pathlib and glob.
✓
Data transforms between CSV, JSON, and more.
✓
Scheduling with cron for time-based automation.
✓
Third-party APIs integrated via requests.
✓
Error handling with logging, retries, and exit codes.

Next module:

5.3 — FastAPI and Web Services: from script to professional API.