Random User Agent Selection and HTTP Request

  • Share this:

Code introduction


This function defines a helper function to randomly select a user agent from a predefined list and then uses this user agent to make an HTTP GET request to a specified URL using the urllib3 library, returning the response data.


Technology Stack : Python, urllib3

Code Type : Function

Code Difficulty : Intermediate


                
                    
import urllib3
import json
import random

def get_random_user_agent():
    # This function fetches a random user agent from a list of popular user agents
    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/44.0.2403.157 UBrowser/44.0.2403.157 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
    ]
    return random.choice(user_agents)

def fetch_random_user_agent():
    # This function uses the get_random_user_agent function to get a random user agent
    # and then creates a custom request with that user agent using the urllib3 library
    http = urllib3.PoolManager()
    user_agent = get_random_user_agent()
    headers = {
        'User-Agent': user_agent
    }
    response = http.request('GET', 'http://httpbin.org/user-agent', headers=headers)
    return response.data                
              
Tags: