You can download this code by clicking the button below.
This code is now available for download.
This function uses the Aiohttp library to randomly select a user agent from a list of user agent strings.
Technology Stack : Aiohttp, asyncio
Code Type : Asynchronous function
Code Difficulty : Intermediate
def random_user_agent():
import random
from aiohttp import ClientSession
async def fetch_user_agent(session):
async with session.get('https://www.useragentstring.com/assets/json/UserAgentStringList.json') as response:
return await response.json()
async def main():
async with ClientSession() as session:
user_agents = await fetch_user_agent(session)
random_agent = random.choice(user_agents)
return random_agent
# Start the event loop
import asyncio
asyncio.run(main())