Python Automation
Web scraping, task automation, and scripting patterns.
Overview
Automate tasks with Python.
Web Scraping
import requests
from bs4 import BeautifulSoup
# Fetch webpage
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract data
title = soup.find('h1').text
print(title)
# Find all links
links = soup.find_all('a')
for link in links:
print(link.get('href'))
File Operations
import os
import shutil
from pathlib import Path
# Create directories
Path('output').mkdir(exist_ok=True)
# Copy files
shutil.copy('source.txt', 'destination.txt')
# Find files
for file in Path('.').glob('*.py'):
print(file.name)
# Process CSV
import csv
with open('data.csv') as f:
reader = csv.DictReader(f)
for row in reader:
print(row['name'], row['email'])
Scheduling Tasks
import schedule
import time
def job():
print("Running scheduled job...")
# Schedule jobs
schedule.every(10).minutes.do(job)
schedule.every().day.at("09:00").do(job)
schedule.every().monday.do(job)
while True:
schedule.run_pending()
time.sleep(1)
Practice
Build a web scraper that collects and processes data.