Dilli Dilli - 1 year ago 114
Python Question

Change proxy after x many loops

I have a list of let's say 100 URLS.
I want to change IP after every 10 URLS.

Let's say I have my own proxies that I'd like to use after each 10 URLS.
How would I use that proxy in my requests - ?

list = [100URLS items]
proxies ['ip:port','ip:port']
for urls in list:
##request 10 URLS here then it might throw me error.
#After it throws me error, I want to be able to use proxies inside a list something like this and reiterate the same request with a new proxy using requests.

Answer Source

import requests

proxy = ''
list = ['http://proxy1','http://proxy2']
count = 0

def proxy_changer():
        proxy_num = count + 1
        proxy = list[proxy_num]
        return proxy
        print "you are out of proxies"

def open_site():
    requests.get('http://example.org', proxies=proxy_changer())

Hopefully this helps as the question is not clear. This will change the proxy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download