最近一周一直在帮家里小弟看高考志愿,所以更新的没那么频繁了,请大家见谅。

在看各高校的往年分数时,忍不住手痒,想着能不能给它爬下来?哈哈,说干就干!

1 流程分析

之前无意中在这个网站发现有各个高校的历年录取分数线:https://gkcx.eol.cn。

我们的目标是用 Python 将下面页面的数据导出到 Excel:

python模拟高考志愿投档 python进高考_数据

这个页面的 URL 是:https://gkcx.eol.cn/schoolhtm/schoolTemple/school160.htm,显然是需要一个 school_id 拼接而成的,那么如何获取这个 school_id 呢?

除非想办法爬取到所有院校的 school_id,这里我想着是从上面图中的搜索框进入:

python模拟高考志愿投档 python进高考_数据_02

这样,整体的业务流程我们就理清楚了:

先调用搜索的 URL 获取到高校的 school_id,拼接到高校的详情访问地址

访问详情地址,抓取目标数据

处理目标数据,存储到 Excel 中

2 获取 school_id

按下 F12,可以看出搜索调用的 URL 是:https://gkcx.eol.cn/soudaxue/queryschool.html?&keyWord1=南京邮电大学,但是我们发现该请求的 response 里并没有高校列表,所以猜测这里是有二次数据请求获取到高校的列表,然后解析显示到页面的。

顺着请求流,我们看到了这么一个请求:

python模拟高考志愿投档 python进高考_XML_03

并且它的 response 刚好是一个包含高校信息的 json,到这里应该还是顺利的,我们只要从这个 json 里解析出我们想要的东西,然后继续后面的步骤就可以了。要注意该请求的 Referer。

但是在解析这个 json 时会遇到一个小问题,返回的数据格式是这样的:

1
2
3
4
5
6
7
8({
"totalRecord": {"num": "2"},
"school": [
{
"schoolid": "160",
"schoolname": "南京邮电大学",
...
});
它是被 (); 包围着的,不是一个合法的 json 数据,这里需要对其进行处理后才能解析 json:
1
2
3# 返回数据包含 ();,需要特殊处理
text = ((response.text).split(');',1)[0]).split('(',1)[1]
j = json.loads(text)

3 分数线获取

学校的详情页面是:https://gkcx.eol.cn/schoolhtm/schoolTemple/school160.htm,同样的套路,在点击后 response 里并没有分数线数据,我想也是二次请求吧,果然在请求流里找到了这个:

python模拟高考志愿投档 python进高考_python例子高考志愿_04

这里的两个请求刚好将高校的每年分数线和各专业的分数线以 XML 的格式返回,Very Good!

下面要做的就是 XML 解析啦。

4 XML 解析
这里我们使用 xml.etree.ElementTree 来解析 XML:
1
2
3
4
5
6
7
8
9
10
2017
软件工程(嵌入式培养)
369
366
364
一批
理科
由于数据比较规整,解析也很简单:
1
2
3
4areapionts = ET.fromstring(response.text)
for areapiont in areapionts:
print(areapiont.find('year').text)
print(areapiont.find('specialname').text)
5 Excel 写入
Excel 的写入需要借助于 openpyxl 模块。
openpyxl 简单使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22>>>import openpyxl
>>>wb = openpyxl.Workbook()
# 初始时会生成一个 sheet 页
>>>wb.sheetnames
['Sheet']
# 创建 sheet 页
>>>wb.create_sheet(index=0,title='First')
# 获取所有 sheet 页
>>>wb.sheetnames
['First', 'Sheet']
# 删除 sheet 页
>>>wb.remove(wb['Sheet'])
>>>wb.sheetnames
['First']
>>>sheet = wb['First']
# 设置单元格
>>>sheet['A1'] = '省份'
>>>sheet['B1'] = '学校'
# 设置指定的单元格
>>>sheet.cell(1,3).value='test'
>>>wb.save('test.xlsx')
XML 解析写入 Excel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23def gen_excel(school,xml,wb):
sheet = wb.create_sheet(title='各专业历年录取分数线')
sheet.column_dimensions['B'].width = 40
sheet['A1'] = '年份'
sheet['B1'] = '专业'
sheet['C1'] = '最高分'
sheet['D1'] = '平均分'
sheet['E1'] = '最低分'
sheet['F1'] = '批次'
sheet['G1'] = '录取批次'
areapionts = ET.fromstring(xml)
column = 1
for areapiont in areapionts:
column += 1
sheet.cell(column,1).value = areapiont.find('year').text
sheet.cell(column,2).value = areapiont.find('specialname').text
sheet.cell(column,3).value = areapiont.find('maxfs').text
sheet.cell(column,4).value = areapiont.find('varfs').text
sheet.cell(column,5).value = areapiont.find('minfs').text
sheet.cell(column,6).value = areapiont.find('pc').text
sheet.cell(column,7).value = areapiont.find('stype').text
wb.save('{}.xlsx'.format(school['schoolname']))
执行效果1
2
3
4$ python gkcx.py
Please the school name:南京邮电大学

共检索到 2 个高校:['南京邮电大学', '南京邮电大学通达学院']

数据获取完成,已下载到脚本目录

python模拟高考志愿投档 python进高考_数据_05

结果看着还可以,但是还是有问题的,因为各省的分数线肯定是不一样的,这里默认检索出的是学校所在省的分数线,因此若要获取在其他省的分数线