File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ import mechanize , cookielib , random
2
+
3
+ class anonBrowser (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
+ def clear_cookies (self ):
17
+ self .cookie_jar = cookielib .LWPCookieJar ()
18
+ self .set_cookiejar (self .cookie_jar )
19
+
20
+ def change_user_agent (self ):
21
+ index = random .randrange (0 , len (self .user_agents ) )
22
+ self .addheaders = [('User-agent' , \
23
+ ( self .user_agents [index ] ))]
24
+
25
+ def change_proxy (self ):
26
+ if self .proxies :
27
+ index = random .randrange (0 , len (self .proxies ))
28
+ self .set_proxies ( {'http' : self .proxies [index ]} )
29
+
30
+ def anonymize (self , sleep = False ):
31
+ self .clear_cookies ()
32
+ self .change_user_agent ()
33
+ self .change_proxy ()
34
+
35
+ if sleep :
36
+ time .sleep (60 )
You can’t perform that action at this time.
0 commit comments