You can download this code by clicking the button below.
This code is now available for download.
This function uses the urllib3 library to generate a random User-Agent header and make an HTTP GET request with a proxy, returning the response data.
Technology Stack : urllib3, random
Code Type : Custom function
Code Difficulty : Intermediate
def get_random_useragent():
import random
from urllib3.util import Retry
from urllib3.util.retry import RetryFromParams
def random_useragent():
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; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36"
]
return random.choice(user_agents)
retries = Retry(
total=3,
read=5,
connect=5,
backoff_factor=0.5,
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "OPTIONS"]
)
proxy = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
with urllib3.PoolManager(
retries=retries,
headers={'User-Agent': random_useragent()},
proxy=proxy
) as http:
response = http.request('GET', 'http://httpbin.org/ip')
return response.data