In [25]:
import requests
import numpy as np
import bs4
url = 'http://www.buffalo.edu/class-schedule?switch=showcourses&semester=fall&division=UGRD&dept=ASL'
s = requests.get(url).text
b = bs4.BeautifulSoup(s,'lxml')
tables =  b.find_all('table')
mytable = tables[5]  # found that we want #5 by trial and error
d = {}

capstart = '''Enrollment Capacity:  
     </td>
     <td class="info">'''
capstop = '<'

enrstart = '''Enrollment Total: &nbsp;
     </td>
     <td class="info">
       '''
enrstop = '<'

fullness = []
for row in mytable:
        if row.name: # exclude null rows that exist for some reason
            tds = row.find_all('td')
            if len(tds)==11: # by experimentation found that true data rows have 11 rtd elements
                course = tds[1].text.strip()
                sec    = tds[3].text.strip()
                days   = tds[5].text.strip().replace(' ','')
                time   = tds[6].text.strip().replace(' ','')
                room   = tds[7].text.strip()
                if room != 'Room':
                    link = row['onclick'].split("'")[1]
                    #print( '\t'.join([course, days, time, room, link]) )
                    page = requests.get(link).text
                    cap = page.split(capstart)[1].split(capstop)[0]
                    enr = page.split(enrstart)[1].split(enrstop)[0]
                    print( '\t'.join([course+' '+sec, days, time, room,  cap, enr, 
                                      str(int(enr)/int(cap))]) )
                    fullness.append( int(enr)/int(cap) )
print(np.array(fullness).mean())
                    
                    
ASL 101LEC A	TR	8:30AM-10:35AM	Park 148	20 	20     	1.0
ASL 101LEC B	TR	11:00AM-1:05PM	Park 250	20 	20     	1.0
ASL 101LEC C	MWF	9:30AM-10:55AM	Park 152	20 	21     	1.05
ASL 101LEC D	MWF	11:00AM-12:25PM	Park 148	22 	22     	1.0
ASL 101LEC E	MWF	1:30PM-2:55PM	Park 152	20 	19     	0.95
ASL 101LEC F	MWF	3:00PM-4:25PM	Park 148	20 	20     	1.0
ASL 101LEC G	MWF	10:30AM-11:55AM	Park 143	20 	20     	1.0
ASL 101LEC H	MW	4:00PM-6:05PM	Park 152	20 	20     	1.0
ASL 101LEC I	TR	4:00PM-6:05PM	Park 152	20 	20     	1.0
ASL 203LEC A	MWF	2:00PM-2:50PM	Park 148	20 	22     	1.1
ASL 203LEC B	TR	2:00PM-3:20PM	Park 143	20 	8     	0.4
ASL 211LEC A	TR	1:30PM-2:50PM	Park 250	36 	8     	0.2222222222222222
0.8935185185185185
In [31]:
import requests
import numpy as np
import bs4

def score(dept):
    url = 'http://www.buffalo.edu/class-schedule?switch=showcourses&semester=fall&division=UGRD&dept='+dept
    s = requests.get(url).text
    b = bs4.BeautifulSoup(s,'lxml')
    tables =  b.find_all('table')
    mytable = tables[5]  # found that we want #5 by trial and error
    d = {}

    capstart = '''Enrollment Capacity: &nbsp;
     </td>
     <td class="info">'''
    capstop = '<'

    enrstart = '''Enrollment Total: &nbsp;
     </td>
     <td class="info">
       '''
    enrstop = '<'

    fullness = []
    for row in mytable:
            if row.name: # exclude null rows that exist for some reason
                tds = row.find_all('td')
                if len(tds)==11: # by experimentation found that true data rows have 11 rtd elements
                    course = tds[1].text.strip()
                    sec    = tds[3].text.strip()
                    days   = tds[5].text.strip().replace(' ','')
                    time   = tds[6].text.strip().replace(' ','')
                    room   = tds[7].text.strip()
                    if room != 'Room':
                        link = row['onclick'].split("'")[1]
                        #print( '\t'.join([course, days, time, room, link]) )
                        page = requests.get(link).text
                        cap = page.split(capstart)[1].split(capstop)[0]
                        enr = page.split(enrstart)[1].split(enrstop)[0]
                        #print( '\t'.join([course+' '+sec, days, time, room,  cap, enr, 
                        #                  str(int(enr)/int(cap))]) )
                        fullness.append( int(enr)/int(cap) )
    #print(np.array(fullness).mean())
    return  np.array(fullness).mean()             
                    
In [33]:
score('MTH')
Out[33]:
0.7610788965408809
In [ ]:
with open('test.txt','w') as f:
    f.write(s)

How to make a random color in hex format?

In [36]:
np.random.randint(0,256)
Out[36]:
117
In [37]:
hex(10)
Out[37]:
'0xa'
In [38]:
hex(10)[2:]
Out[38]:
'a'
In [51]:
def randomhexcolor():
    r,g,b = np.random.randint(0,256,3)
    return '#'+hex(r)[2:].zfill(2)+hex(g)[2:].zfill(2)+hex(b)[2:].zfill(2)
    
randomhexcolor()
Out[51]:
'#d51479'
In [48]:
for i in range(19):
    print(i,hex(i)[2:].zfill(2))
0 00
1 01
2 02
3 03
4 04
5 05
6 06
7 07
8 08
9 09
10 0a
11 0b
12 0c
13 0d
14 0e
15 0f
16 10
17 11
18 12
In [ ]:
with open('mybubbles.svg','w') as f:
    # write header stuff
    # write a bunch of circles
    # write closing tag
    f.write(stuff)
    print(stuff, file=f)
In [54]:
'Hello {:>20} there {} bye {}'.format(34,'wow',766)
Out[54]:
'Hello                   34 there wow bye 766'
In [56]:
for i in range(8,13):
    print('Hello {:>4} there {} bye {}'.format(i,'wow',766))
Hello    8 there wow bye 766
Hello    9 there wow bye 766
Hello   10 there wow bye 766
Hello   11 there wow bye 766
Hello   12 there wow bye 766
In [57]:
bs4.__version__
Out[57]:
'4.6.0'
In [ ]: