|
| 1 | +importmechanize,cookielib,random |
| 2 | + |
| 3 | +classanonBrowser(mechanize.Browser): |
| 4 | + |
| 5 | +def__init__(self,proxies= [],user_agents= []): |
| 6 | +mechanize.Browser.__init__(self) |
| 7 | +self.set_handle_robots(False) |
| 8 | +self.proxies=proxies |
| 9 | +self.user_agents=user_agents+ ['Mozilla/4.0 ',\ |
| 10 | +'FireFox/6.01','ExactSearch','Nokia7110/1.0'] |
| 11 | + |
| 12 | +self.cookie_jar=cookielib.LWPCookieJar() |
| 13 | +self.set_cookiejar(self.cookie_jar) |
| 14 | +self.anonymize() |
| 15 | + |
| 16 | +defclear_cookies(self): |
| 17 | +self.cookie_jar=cookielib.LWPCookieJar() |
| 18 | +self.set_cookiejar(self.cookie_jar) |
| 19 | + |
| 20 | +defchange_user_agent(self): |
| 21 | +index=random.randrange(0,len(self.user_agents) ) |
| 22 | +self.addheaders= [('User-agent', \ |
| 23 | + (self.user_agents[index] ))] |
| 24 | + |
| 25 | +defchange_proxy(self): |
| 26 | +ifself.proxies: |
| 27 | +index=random.randrange(0,len(self.proxies)) |
| 28 | +self.set_proxies( {'http':self.proxies[index]} ) |
| 29 | + |
| 30 | +defanonymize(self,sleep=False): |
| 31 | +self.clear_cookies() |
| 32 | +self.change_user_agent() |
| 33 | +self.change_proxy() |
| 34 | + |
| 35 | +ifsleep: |
| 36 | +time.sleep(60) |