75 MINS

Build With Me: News Scraper & Digest

Build With Me 03

The Daily Briefing Pipeline

Today we are building an end-to-end data pipeline. We will scrape top headlines from HackerNews, summarize them using an LLM API, and automatically dispatch a formatted HTML email digest to our subscribers.

Step 1: Extract & Transform

Text
import requests
from bs4 import BeautifulSoup

def scrape_headlines():
    print(Fetching headlines...)
    url = https://news.ycombinator.com/
    response = requests.get(url)
    soup = BeautifulSoup(response.text, html.parser)
    
    articles = []
    # Find the title rows
    for item in soup.find_all(tr, class_=athing)[:5]: # Get top 5
        title_element = item.find(span, class_=titleline).find(a)
        title = title_element.text
        link = title_element[href]
        articles.append({title: title, link: link})
        
    return articles

Step 2: Generate the Digest & Load (Email)

Text
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

def send_digest(articles):
    print(Formatting digest...)
    
    html_content = "<h2>Your Daily Tech Briefing</h2><ul>"
    for article in articles:
        html_content += f"<li><a href='{article['link']}'>{article[title]}</a></li>"
    html_content += "</ul>"
    
    # Email Setup
    msg = MIMEMultipart()
    msg[Subject] = Daily HackerNews Digest
    msg[From] = bot@voidxhqhq.com
    msg[To] = subscriber@example.com
    
    msg.attach(MIMEText(html_content, html))
    
    print(Connecting to SMTP server...)
    # Example using Mailtrap or standard SMTP
    # with smtplib.SMTP(smtp.mailtrap.io, 2525) as server:
    #     server.login(user, password)
    #     server.send_message(msg)
        
    print(Digest successfully delivered!)

# Run the pipeline
news = scrape_headlines()
send_digest(news)

Knowledge Check

Ready to test your understanding of Build With Me: News Scraper & Digest?

Back