Last active
January 13, 2019 20:21
-
-
Save ShaikeA/d098e62b95b6fdac02a7f74c385964f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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