-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.py
34 lines (29 loc) · 939 Bytes
/
browser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import mechanize
import cookielib
import time
class Browser:
def __init__(self):
self._br = mechanize.Browser()
self._set_cookie_jar()
self._set_options()
def _set_cookie_jar(self):
cj = cookielib.LWPCookieJar()
self._br.set_cookiejar(cj)
def _set_options(self):
self._br.set_handle_equiv(True)
self._br.set_handle_redirect(True)
self._br.set_handle_referer(True)
self._br.set_handle_robots(False)
self._br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
def _get_page_content(self, url):
okay = False
while not okay:
try:
page = self._br.open(url)
okay = True
except:
print url
print 'mechanize error... sleep for 10 seconds'
time.sleep(10)
content = page.read()
return content