You can download this code by clicking the button below.
This code is now available for download.
This code uses the Aiohttp library to asynchronously fetch data from a random user data API and prints out the user's information.
Technology Stack : Aiohttp, asyncio
Code Type : Asynchronous network request
Code Difficulty : Intermediate
import aiohttp
import asyncio
import random
async def fetch_random_user_data(session):
url = "https://randomuser.me/api/"
async with session.get(url) as response:
return await response.json()
async def print_user_data(user_data):
for user in user_data['results']:
print(f"Name: {user['name']['title']} {user['name']['first']} {user['name']['last']}")
print(f"Email: {user['email']}")
print(f"Phone: {user['phone']}")
print(f"City: {user['location']['city']}")
print(f"Country: {user['location']['country']}")
print("----------")
async def main():
async with aiohttp.ClientSession() as session:
user_data = await fetch_random_user_data(session)
await print_user_data(user_data)
# Code information