Summarizing Web Page Content with Claude Haiku
Summarizing Web Page Content with Claude Haiku
In this recipe, we'll learn how to fetch the content of a web page given its URL and then use Anthropic's Claude API to generate a summary of the page's content.
Let's start by installing the Anthropic library.
Setup
Setup
First, let's install the necessary libraries and setup our Anthropic client with our API key.
# Install the necessary libraries
%pip install anthropic
# Import the required libraries
from anthropic import Anthropic
# Set up the Claude API client
client = Anthropic()
MODEL_NAME = "claude-haiku-5-5"
Step 1: Fetching the Web Page Content
Step 1: Fetching the Web Page Content
First, we need to fetch the content of the web page using the provided URL. We'll use the requests library for this purpose.
import requests
url = "https://en.wikipedia.org/wiki/96th_Academy_Awards"
# Wikimedia asks clients to send an identifying User-Agent; the default python-requests one is refused (403)
headers = {
"User-Agent": "claude-cookbook-example/1.0 (https://github.com/anthropics/claude-cookbooks)"
}
response = requests.get(url, headers=headers, timeout=30)
if response.status_code == 200:
page_content = response.text
else:
print(f"Failed to fetch the web page. Status code: {response.status_code}")
exit(1)
Step 2: Preparing the Input for Claude
Step 2: Preparing the Input for Claude
Next, we'll prepare the input for the Claude API. We'll create a message that includes the page content and a prompt asking Claude to summarize it.
prompt = (
f"<content>{page_content}</content>Please produce a concise summary of the web page content."
)
messages = [{"role": "user", "content": prompt}]
Step 3: Generating the Summary
Step 3: Generating the Summary
Now, we'll call Haiku to generate a summary of the web page content.
response = client.messages.create(model=MODEL_NAME, max_tokens=2048, messages=messages)
# Read the text block by type: the response may start with a thinking block
summary = "".join(block.text for block in response.content if block.type == "text")
print(summary)
**96th Academy Awards (Wikipedia article)** The 96th Academy Awards ceremony, presented by the Academy of Motion Picture Arts and Sciences, was held on March 10, 2024, at the Dolby Theatre in Hollywood, Los Angeles. It honored films released in 2023 and was hosted by Jimmy Kimmel, his fourth time hosting. The telecast was produced by Raj Kapoor and Katy Mullan, directed by Hamish Hamilton, and aired on ABC. Key points: - **Top winners:** *Oppenheimer* won seven awards, including Best Picture and Best Director (Christopher Nolan). *Poor Things* won four, and *The Zone of Interest* won two. *Oppenheimer* also led in nominations with 13, ahead of *Poor Things* (11). - **Acting:** Cillian Murphy (Best Actor, *Oppenheimer*), Emma Stone (Best Actress, *Poor Things*), Robert Downey Jr. (Best Supporting Actor, *Oppenheimer*), and Da'Vine Joy Randolph (Best Supporting Actress, *The Holdovers*). - **Other notable wins:** Billie Eilish and Finneas O'Connell won Best Original Song for "What Was I Made For?" from *Barbie*, and *The Zone of Interest* won Best International Feature Film. *Godzilla Minus One* won Best Visual Effects. - **Records and firsts:** Martin Scorsese became the oldest Best Director nominee, and three Best Picture nominees were directed by women. - **Ceremony details:** The show started an hour earlier than usual. Presenters and performers included Ryan Gosling performing "I'm Just Ken." The "In Memoriam" segment featured Andrea Bocelli. - **Controversy:** Jonathan Glazer's acceptance speech about the Israel–Gaza conflict drew praise and criticism, and led to open letters from both supporters and critics. - **Reception:** The telecast drew about 19.49 million U.S. viewers, a 4% increase over the previous year. It later won four Primetime Emmys. The page also covers the Governors Awards (held January 9, 2024), the Academy's new Best Picture diversity standards, and box office performance of the nominees.