-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebutils.py
37 lines (29 loc) · 1.1 KB
/
webutils.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
34
35
36
37
import os
import urllib2
import cookielib
class WebUtils(object):
""" Utils for logging in and retrieving/posting stuff to BrightShadows
"""
def __init__(self, cookiefile, base_url):
self.cookie_jar = cookielib.MozillaCookieJar(cookiefile)
self.base_url = base_url
print "baseurl = ", self.base_url
if(os.access(cookiefile, os.F_OK)):
self.cookie_jar.load()
self.opener = urllib2.build_opener(
urllib2.HTTPRedirectHandler(),
urllib2.HTTPHandler(debuglevel=0),
urllib2.HTTPSHandler(debuglevel=0),
urllib2.HTTPCookieProcessor(self.cookie_jar)
)
def getPNG(self, pagePath, targetFilename):
print "reading path " + pagePath
response = self.opener.open(self.base_url + pagePath)
pic = response.read()
fout = open(targetFilename, "wb")
fout.write(pic)
fout.close()
print "file written to " + targetFilename
def getHTML(self, pagePath):
response = self.opener.open(self.base_url + pagePath)
return ''.join(response.readlines())