看到很多同学等南航计算机学院出复试名单等得心累,正好最近在写一个爬虫的程序,所以我干脆写了一个南航计算机学院官网的脚本,实时监控复试信息。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 18 16:02:25 2018

用来自动检测 复试信息 的脚本

当监测到官网的招生信息更新后,电脑会发出报警声,然后直接去官网查看信息即可

用到了urllib包和beautifulsoap包

@author: wansho
"""

from urllib.request import urlopen
from urllib.request import Request

from bs4 import BeautifulSoup

import time

import winsound

def play_music():
while True:
winsound.PlaySound('alert', winsound.SND_ASYNC)
time.sleep(1)

oldNews = []

flag = 0


while True:

flag = flag + 1

print("\n--------第 " + str(flag) + ' 次扫描,无更新-------\n')

url = 'http://cs.nuaa.edu.cn/?cat=65'

headers = {
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',

'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Cookie':'_gscu_463181622=07464103q151k172; _ga=GA1.3.929626364.1513514527; Coremail=c7db586ffe6b735ff17344f754e33fa2; iPlanetDirectoryPro=AQIC5wM2LY4Sfcy5MUnAg%2BW2BwJaL%2BCyTRPZcSZRzbk3VIY%3D%40AAJTSQACMDE%3D%23',
'Host':'cs.nuaa.edu.cn',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}

req = Request(url=url, headers=headers)

html_str = urlopen(req).read().decode('utf-8')

soup = BeautifulSoup(html_str,'html.parser', from_encoding="gb18030")

fixed_html = soup.prettify()

soup = BeautifulSoup(fixed_html)

infos = soup.find('div', attrs = {'class' : 'maincontent'})

sections = infos.find_all('tr', attrs = {'class' : 'section'})
alts = infos.find_all('tr', attrs = {'class' : 'alt'})

news = []

for section,alt in zip(sections, alts):
title1 = section.find('a').get_text().strip()
title2 = alt.find('a').get_text().strip()
news.append(title1)
news.append(title2)

if flag == 1: # 第一次爬取
oldNews = news
else:
for new in news:
if new not in oldNews:# 有更新了
print("\n\n---------------更新了---------------\n\n")
print("标题为: " + new)
print("\n\n---------------更新了---------------\n\n")
play_music()

# 一分钟扫描一次
sleep_time = 60

time.sleep(sleep_time)