Dilli Dilli - 1 month ago 19
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:
try:
##request 10 URLS here then it might throw me error.
except:
#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
#!/usr/bin/python

import requests

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

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


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

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

Comments