Skip to content

Instantly share code, notes, and snippets.

@ShaikeA
Last active January 13, 2019 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShaikeA/d098e62b95b6fdac02a7f74c385964f1 to your computer and use it in GitHub Desktop.
Save ShaikeA/d098e62b95b6fdac02a7f74c385964f1 to your computer and use it in GitHub Desktop.
from itertools import cycle
import requests
# Generate the pools
def create_pools():
proxies = proxies_pool()
headers = [random_header() for ind in range(len(proxies))] # list of headers, same length as the proxies list
# This transforms the list into itertools.cycle object (an iterator) that we can run
# through using the next() function in lines 16-17.
proxies_pool = cycle(proxies)
headers_pool = cycle(headers)
return proxies_pool, headers_pool
# Usage example
proxies_pool, headers_pool = create_pools()
current_proxy = next(proxy_pool)
current_headers = next(headers_pool)
# Introduce the proxy and headers in the GET request
with requests.Session() as req:
page = req.get(link, proxies={"http": current_proxy, "https": current_proxy},
headers=current_headers, timeout=30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment