Random User Agent Fetcher using Aiohttp

  • Share this:

Code introduction


This function uses the Aiohttp library to fetch a random user agent from a user agent string API.


Technology Stack : Aiohttp, Python asyncio

Code Type : Asynchronous function

Code Difficulty : Intermediate


                
                    
def get_random_user agent():
    import aiohttp
    import random
    import json

    async def fetch_user_agent():
        async with aiohttp.ClientSession() as session:
            async with session.get('https://api.useragentstring.com/random') as response:
                return await response.json()

    user_agent_data = await fetch_user_agent()
    return user_agent_data['user_agent']