2016-10-29 26 views
-1

나는 100 개의 URL을 사용한다고 가정 해 보겠습니다. 10 개의 URL마다 IP를 변경하고 싶습니다.x가 여러 번 반복 된 후 프록시 변경

각 10 개의 URL 후에 사용하려는 프록시가 있습니다. 요청에 해당 프록시를 어떻게 사용합니까?

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. 

답변

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') 

여기에 전체 코드입니다. open_site(' http://google.com ')을 사용하는 10 페이지 이후에 프록시를 변경해야합니다. 프록시가 부족하면 예외가 반환됩니다.