Random User Agent and URL Fetching Simulation

  • Share this:

Code introduction


This function uses the requests library and the fake_useragent library to simulate a random user agent and fetch the content of a specified URL.


Technology Stack : requests, fake_useragent

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def get_random_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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 UBrowser/88.0.4324.150 Safari/537.36",
        "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 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
    ]
    return random.choice(user_agents)

def fetch_random_page(url):
    from requests import get
    from fake_useragent import UserAgent
    
    user_agent = UserAgent()
    headers = {'User-Agent': user_agent.random}
    
    response = get(url, headers=headers)
    return response.text