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 a random user agent from a user agent string API.
Technology Stack : Aiohttp, Python asyncio
Code Type : Asynchronous HTTP request
Code Difficulty : Intermediate
def random_useragent():
import aiohttp
import random
from aiohttp import ClientSession
async def fetch_useragent(session):
async with session.get('https://api.useragentstring.com/random') as response:
return await response.json()
async def main():
async with ClientSession() as session:
useragent = await fetch_useragent(session)
return useragent['user_agent']
# Run the main coroutine
import asyncio
asyncio.run(main())