-1

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. 
Biplov
  • 1,136
  • 1
  • 20
  • 44

1 Answers1

2
#!/usr/bin/python

import requests

class Proxer:
    proxy = ''
    list = ['http://proxy1','http://proxy2', 'http://pox']
    proxy_count = 0
    page_count = 0

    def proxy_changer(self):
        try:
            if self.proxy_count > 0:
                self.proxy_count = self.proxy_count + 1
            self.proxy = self.list[self.proxy_count]
            return self.proxy
        except:
            print "you are out of proxies"


    def open_site(self, url):
        self.page_count = self.page_count + 1
        try:
            if self.page_count%10:
                self.proxy_changer()
        except:
            pass
        requests.get(url, {'http':self.proxy})


Proxer().open_site('http://google.com')

Here is the full code. Should change the proxy after 10 pages using the open_site('http://google.com') Once you run out of proxies a exception will be returned.

Jasmit Tarang
  • 135
  • 1
  • 7