Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Async Web Scraper with Rate Limiting | Advanced Control and Debugging
Python Asyncio in Depth
Abschnitt 4. Kapitel 3
single

single

Challenge: Async Web Scraper with Rate Limiting

Swipe um das Menü anzuzeigen

You are building a production-ready async data pipeline that fetches posts and their comments from jsonplaceholder.typicode.com. The pipeline must handle concurrency, apply rate limiting, enforce timeouts, and gracefully recover from individual failures.

The following endpoints are available:

  • https://jsonplaceholder.typicode.com/posts/{post_id} – returns a post object with title and userId fields;
  • https://jsonplaceholder.typicode.com/comments?postId={post_id} – returns a list of comments for a given post.
Aufgabe

Wischen, um mit dem Codieren zu beginnen

  1. Define an async function fetch_post(client, semaphore, post_id) that:
    • uses the provided semaphore to limit concurrency;
    • fetches the post with a 4.0 second timeout using asyncio.wait_for();
    • returns a dict with keys post_id, title, and user_id;
    • returns None on any exception.
  2. Define an async function fetch_comment_count(client, semaphore, post_id) that:
    • uses the provided semaphore to limit concurrency;
    • fetches the comments list with a 4.0 second timeout;
    • returns the number of comments as an integer;
    • returns 0 on any exception.
  3. Define an async function build_report(client, semaphore, post_id) that:
    • calls fetch_post() and fetch_comment_count() concurrently using asyncio.gather();
    • returns None if fetch_post() returned None;
    • otherwise returns a formatted string: "[User {user_id}] {title} – {comment_count} comments".
  4. Define an async function main() that:
    • creates an asyncio.Semaphore with a limit of 5;
    • builds reports for post IDs 1 through 10 concurrently using asyncio.gather();
    • prints each non-None result on a separate line.
  5. Run main() using asyncio.run().

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt