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

single

Challenge: Async Web Scraper with Rate Limiting

Svep för att visa menyn

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.
Uppgift

Svep för att börja koda

  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ösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 3
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt