Random User Agent Selection from JSON File

  • Share this:

Code introduction


The function uses the requests library to read a list of user agents from a local JSON file and returns a random one.


Technology Stack : Python, requests, json

Code Type : Function

Code Difficulty : Intermediate


                
                    
def get_random_useragent():
    import random
    import requests
    import json

    def get_useragents():
        with open('user_agents.json', 'r') as file:
            user_agents = json.load(file)
        return user_agents

    user_agents = get_useragents()
    return random.choice(user_agents)