Skip to content

Instantly share code, notes, and snippets.

@ltiao
Last active December 12, 2015 12:09
Show Gist options
  • Save ltiao/4770425 to your computer and use it in GitHub Desktop.
Save ltiao/4770425 to your computer and use it in GitHub Desktop.
prints list of prescribed textbooks given a list of subjects
#!/usr/bin/python
import pprint, requests, sys
from bs4 import BeautifulSoup
from itertools import izip
base_url = 'http://www.bookshop.unsw.edu.au/cgi-bin/bookweb/subject2'
subjects = []
while True:
try:
value = raw_input()
subjects.append(value)
except EOFError:
break
except KeyboardInterrupt:
print 'See you soon'
sys.exit()
for s in subjects:
payload = {'subject1': s, 'Submit': 'Submit Query',}
r = requests.get(base_url, params=payload)
list_soup = BeautifulSoup(r.text)
rows = list_soup.find_all('tr')[1:]
header = rows.pop(0)
print header.b.string
for row in rows:
for title in row.find_all('a'):
book_detail_request = requests.get(title.get('href'))
book_soup = BeautifulSoup(book_detail_request.text)
strings = book_soup.table.stripped_strings
i = iter(strings)
b = dict(izip(i, i))
print '* Title: {title}\n APN: {apn}\n Price: {price}\n Publisher: {publisher}\tEdition: {edition}\tAuthor: {author}'.format(title = b['Title:'], apn = b['APN:'], price = b['Our Price:'], publisher = b['Publisher:'], edition = b['Edition:'], author = b['Author:'])
@ltiao
Copy link
Author

ltiao commented Feb 14, 2013

I now use requests cause that shit's money as hell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment