Skip to content

Instantly share code, notes, and snippets.

@ltiao
Created March 2, 2013 02:12
Show Gist options
  • Save ltiao/5069367 to your computer and use it in GitHub Desktop.
Save ltiao/5069367 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import requests, re, json
from bs4 import BeautifulSoup
r = requests.get('https://my.unsw.edu.au/amserver/UI/Login?module=ISISWSSO&IDToken1=')
soup = BeautifulSoup(r.text)
form = soup.find("form", {"id": "muLoginForm"})
payload = {}
for field in form.find_all("input"):
if field['type'] in ('text', 'hidden', 'password', 'submit'):
if field.has_key('value'):
payload[field['name']] = field['value']
else:
payload[field['name']] = ''
payload['username'], payload['password'] = '', ''
s = requests.Session()
s.post(r.url, data=payload)
r = s.get('https://my.unsw.edu.au/active/studentTimetable/timetable.xml')
soup = BeautifulSoup(r.text)
thing = {}
for d in soup.find_all("td", {"class": "sectionHeading"}):
code, name = d.text.split(' - ', 1)
thing[code] = dict(zip([i.text.lower() for i in d.parent.parent.find_all('td', {'class': 'label'})], [i.text for i in d.parent.parent.find_all('td', {'class': 'data'})]))
thing[code]['name'] = name
header = d.parent.parent.next_sibling.next_sibling.find('tr')
keys = [k for key in header for k in key.text.strip().lower().split('/', 1)]
classes = []
for i in d.parent.parent.next_sibling.next_sibling.find_all('tr', {'class': re.compile('row(Low|High)light')}):
values = [value.text.strip() for value in i]
if not values[0]:
last.extend(values[1:])
values = last
last = values[0:2]
classes.append(dict(zip(keys, values)))
thing[code]['classes'] = classes
print json.dumps(thing, sort_keys=True, indent=4, separators=(',', ': '))
# r = s.get('https://my.unsw.edu.au/active/studentStatement/statement.xml')
# soup = BeautifulSoup(r.text)
# print soup.prettify()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment