Created
February 27, 2011 12:42
-
-
Save sunng87/846150 to your computer and use it in GitHub Desktop.
Seat Query of TOEFL in China
This file contains hidden or 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/python3 | |
import urllib.parse | |
import http.client | |
import html.parser | |
from datetime import datetime | |
def get_seat_status(location, month): | |
""" get free seat status for given location(Beijing) and month(201104) | |
""" | |
params = {'mvfSiteProvinces':location, 'mvfAdminMonths':month, | |
'whichFirst':'AS', '__act':'__id.22.SeatsQuery.adp.actList'} | |
encoded_params = urllib.parse.urlencode(params) | |
try: | |
conn = http.client.HTTPConnection('toefl1.etest.net.cn') | |
conn.request('POST', '/cn/SeatsQuery', encoded_params, | |
{'Content-Type':'application/x-www-form-urlencoded'}) | |
results = conn.getresponse().read() | |
conn.close() | |
results = str(results, 'GB2312') | |
parser = DataParser() | |
parser.feed(results) | |
return parser.values | |
except http.client.HTTPException: | |
return {} | |
class DataParser(html.parser.HTMLParser): | |
def __init__(self): | |
html.parser.HTMLParser.__init__(self) | |
self.values = [] | |
self._current_entry = None | |
self._current_date = None | |
self.is_date = False | |
self.is_empty = False | |
self.is_id = False | |
self.is_addr = False | |
self.is_fee = False | |
self.is_status = False | |
def handle_starttag(self, tag, attrs): | |
if tag == 'tr' and len(attrs) > 0: | |
attr = attrs[0] | |
if attr[0] == 'bgcolor' and attr[1] == '#E0E0E0': | |
self.is_date = True | |
if attr[0] == 'bgcolor' and attr[1] == '#CCCCCC': | |
self.is_empty = True | |
def handle_data(self, data): | |
if self.is_date: | |
self.is_date = False | |
self._current_date = datetime.strptime(data, "%Y年%m月%d日 %H:%M") | |
return | |
if self.is_empty: | |
self.is_empty = False | |
self.is_id = True | |
return | |
if self.is_id and data.strip() != '': | |
self.is_id = False | |
self._current_entry = [self._current_date, data,] | |
self.is_addr = True | |
return | |
if self.is_addr and data.strip() != '': | |
self.is_addr = False | |
self._current_entry.append(data) | |
self.is_fee = True | |
return | |
if self.is_fee and data.strip() != '': | |
self.is_fee = False | |
self._current_entry.append(data) | |
self.is_status = True | |
return | |
if self.is_status and data.strip() != '': | |
self.is_status = False | |
if data == '有名额': | |
self._current_entry.append(True) | |
else: | |
self._current_entry.append(False) | |
self.values.append(self._current_entry) | |
return | |
if __name__ == '__main__': | |
import pprint | |
v = get_seat_status('Jiangsu', '201104') | |
pprint.pprint(v) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment