Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Async Web Scraper with Rate Limiting | Advanced Control and Debugging
Python Asyncio in Depth
Seção 4. Capítulo 3
single

single

Challenge: Async Web Scraper with Rate Limiting

Deslize para mostrar o menu

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

Deslize para começar a programar

  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().

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt