Last active
December 12, 2015 12:09
-
-
Save ltiao/4770425 to your computer and use it in GitHub Desktop.
prints list of prescribed textbooks given a list of subjects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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:']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I now use requests cause that shit's money as hell.