Random User Agent Generator

  • Share this:

Code introduction


This function is used to randomly generate a user agent string, which is typically used in web requests to simulate different browser environments.


Technology Stack : requests, requests.utils, random

Code Type : The type of code

Code Difficulty :


                
                    
def random_user_agent():
    import random
    from requests.utils import default_user_agent

    user_agents = ['Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
                   'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15',
                   'Mozilla/5.0 (Linux; Android 10; SM-A505FN) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.93 Mobile Safari/537.36']

    return random.choice(user_agents)