用python自动获取图书馆空座信息

2016-02-27  本文已影响129人  George_Lee

图书馆有个长这个样子的网页。。。我有点想知道去几楼会比较空,什么时间去座位比较多。又懒得不停地查这个网页,那只能编个程序让它替我干这个琐事了。。。

结论:从人数变动图来看,今天的学霸们从8:15就开始涌入图书馆了,人数直到17:00才开始下降,晚上又有小幅度上升。

在中午的就餐时间出去的人比较少,应该是下午还要学习。而晚饭时间出去的人多很多,而且一去不回,大概是学了一天回去休息了。晚饭后又涌进来一些人,大概学到21:00就大部分开始回去了。

按照空座/有人座位来看,一楼晚上坐着最空,其次3楼和四楼差不多,比较空吧。

所有图表、代码在下面:

还好这个网页还比较好看懂。。。

程序代码:

需要python2.7 BeautifulSoup支持

# get library seats

# Designed By Xc.Li

import urllib

import BeautifulSoup

import re

import time

import os

def find_number(keyword):

for tag in tags:

s_tag = str(tag)

flag = re.match(keyword,s_tag)

if flag is not None:

return s_tag[54:-18]

while (True):

url = 'http://lib.ecust.edu.cn:8081/gateseat/lrp.aspx'

html = urllib.urlopen(url).read()

soup = BeautifulSoup.BeautifulSoup(html)

tags = soup('span')

output = list()

output.append(time.ctime()[11:-4])

for floor in [1, 2, 3, 4, 5, 6]:

# print 'Floor:', floor

keyword = '^'

# print 'Used:', find_number(keyword)

output.append(int(find_number(keyword)))

keyword = '^'

# print 'Left:', find_number(keyword)

output.append(int(find_number(keyword)))

print output

tf = open('lib-rec.csv','a')

for data in output:

tf.write(str(data))

tf.write(',')

tf.write('\n')

tf.close()

i = -5

while (i<=900):

i = i+5

print 'Running!'

print 900-i,'seconds left'

time.sleep(5)

os.system('cls')

上一篇下一篇

猜你喜欢

热点阅读