Python爬虫爬取全国各大高校各专业分数

本文仅练习爬虫程序的编写,并无保存任何数据,网址接口已经打码处理。

目标:http://xxx.com


我们通过分析网络请求可以看到有这两个json文件:

https://xxx.cn/www/2.0/schoolprovinceindex/2018/318/12/1/1.json
https://xxx..cn/www/2.0/schoolspecialindex/2018/31/11/1/1.json

其中318是学校id,12是省份id,代表的是天津
分别对应着学校各省分数线以及和各专业分数线




因此我们当前页面的代码为:

import requests

HEADERS = {
    "Accept": "text/html,application/xhtml+xml,application/xml;",
    "Accept-Language": "zh-CN,zh;q=0.8",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
    'Referer': 'https://xxx.cn/school/search'
}
url = 'https://xxx.cn/www/2.0/schoolprovinceindex/2018/1217/12/1/1.json'
response = requests.get(url,headers=HEADERS)
print(response.json())

接下来我们就要想办法获取学校id了,同样我们分析到:

https://xxxl.cn/gkcx/api/?uri=apigkcx/api/school/hotlists

通过post如下数据:

data = {"access_token":"","admissions":"","central":"","department":"","dual_class":"","f211":"","f985":"","is_dual_class":"","keyword":"","page":2,"province_id":"","request_type":1,"school_type":"","size":20,"sort":"view_total","type":"","uri":"apigkcx/api/school/hotlists"}

我们可以看到一个参数是page,对应着页码:
所以我们这部分的代码为:

import requests

HEADERS = {
    "Accept": "text/html,application/xhtml+xml,application/xml;",
    "Accept-Language": "zh-CN,zh;q=0.8",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
    'Referer': 'https://xxx.cn/school/search'
}
url = 'https://xxx.cn/gkcx/api/?uri=apigkcx/api/school/hotlists'
data = {"access_token":"","admissions":"","central":"","department":"","dual_class":"","f211":"","f985":"","is_dual_class":"","keyword":"","page":2,"province_id":"","request_type":1,"school_type":"","size":20,"sort":"view_total","type":"","uri":"apigkcx/api/school/hotlists"}
response = requests.post(url,headers=HEADERS,data=data)
print(response.json())

我们处理一下就可以获得学校的id,为了美观和之后数据处理我们加到字典里,

import requests

HEADERS = {
    "Accept": "text/html,application/xhtml+xml,application/xml;",
    "Accept-Language": "zh-CN,zh;q=0.8",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
    'Referer': 'https://xxx.cn/school/search'
}

school_info = []
def get_schoolid(pagenum):
    url = 'https://xxx.cn/gkcx/api/?uri=apigkcx/api/school/hotlists'
    data = {"access_token":"","admissions":"","central":"","department":"","dual_class":"","f211":"","f985":"","is_dual_class":"","keyword":"","page":pagenum,"province_id":"","request_type":1,"school_type":"","size":20,"sort":"view_total","type":"","uri":"apigkcx/api/school/hotlists"}
    response = requests.post(url,headers=HEADERS,data=data)
    school_json = response.json()
    schools = school_json['data']['item']
    for school in schools:
        school_id = school['school_id']
        school_name = school['name']
        school_dict = {
        'id':school_id,
        'name':school_name
        }
        school_info.append(school_dict)

def main():
    get_schoolid(2)
    print(school_info)
if __name__ == '__main__':
    main()

结果如下:



因为之后我们想要遍历所有页面的学校id,所以保留了一个pagenum参数,用作循环。
接下来就是添加上获取相应简略信息以及详细专业分数:

import requests

HEADERS = {
    "Accept": "text/html,application/xhtml+xml,application/xml;",
    "Accept-Language": "zh-CN,zh;q=0.8",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0",
    'Referer': 'https://xxx.cn/school/search'
}

school_info = []
simple_list = []
pro_list = []
name_list = []
def get_schoolid(pagenum):
    url = 'https://xxx.cn/gkcx/api/?uri=apigkcx/api/school/hotlists'
    data = {"access_token":"","admissions":"","central":"","department":"","dual_class":"","f211":"","f985":"","is_dual_class":"","keyword":"","page":pagenum,"province_id":"","request_type":1,"school_type":"","size":20,"sort":"view_total","type":"","uri":"apigkcx/api/school/hotlists"}
    response = requests.post(url,headers=HEADERS,data=data)
    school_json = response.json()
    schools = school_json['data']['item']
    for school in schools:
        school_id = school['school_id']
        school_name = school['name']
        school_dict = {
        'id':school_id,
        'name':school_name
        }
        school_info.append(school_dict)

def get_info(id,name):
    simple_url  = 'https://xxx.cn/www/2.0/schoolprovinceindex/2018/%s/12/1/1.json'%id
    simple_response = requests.get(simple_url,headers=HEADERS)
    simple_info = simple_response.json()['data']['item'][0]
    simple_infodict = {
        'name':name,
        'max':simple_info['max'],
        'min':simple_info['min'],
        'average':simple_info['average'],
        'local_batch_name':simple_info['local_batch_name']
    }
    simple_list.append(simple_infodict)
def get_score(id,name):
    professional_url  = 'https://xxx.cn/www/2.0/schoolspecialindex/2018/%s/12/1/1.json'%id
    professional_response = requests.get(professional_url,headers=HEADERS)
    for pro_info in professional_response.json()['data']['item']:
        pro_dict = {
        'name':name,
        'spname':pro_info['spname'],
        'max':pro_info['max'],
        'min':pro_info['min'],
        'average':pro_info['average'],
        'min_section':pro_info['min_section'],
        'local_batch_name':pro_info['local_batch_name']
        }
        pro_list.append(pro_dict)

def main():
    print('\033[0;36m='*15+'2018全国高校录取分数信息查询系统'+'='*15+'\033[0m'+'\n')
    get_schoolid(1)
    for school in school_info:
        id = school['id']
        name = school['name']
        try:
            get_info(id,name)
            print('[*]正在抓取2018%s在天津市录取分数信息'%name)
        except:
            print('[*]%s暂时未查到录取分数信息'%name)
        try:
            get_score(id,name)
            print('[*]正在抓取2018%s专业分数线信息'%name)
        except:
            print('[*]%s暂时未查专业分数线信息'%name)

    print('\033[0;36m[*]信息抓取结束,即将开始整理信息\033[0m')
    print('\033[0;36m[*]即将展示天津市各高校2018分数信息\033[0m')
    for school in simple_list:
        print('学校名称:{name},最高分:{max},最低分:{min},平均分:{average}'.format(**school))
    print('\033[0;36m[*]即将展示天津市各高校2018专业分数线信息\033[0m')
    for school in pro_list:
        print('学校名称:{name},专业名称:{spname},最高分:{max},最低分:{min},平均分:{average},最低位次:{min_section}'.format(**school))
if __name__ == '__main__':
    main()


因为一共有142页,io密集型可以使用多线程提高爬虫速度,但是要注意共同变量的问题,由于之前总结过python多线程的相关内容,接下来我们可以通过pandas保存到excel,我们可以先将字典转换成dataframe,然后保存为excel。



也可以通过pyecharts等进行数据分析。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,463评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,868评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,213评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,666评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,759评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,725评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,716评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,484评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,928评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,233评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,393评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,073评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,718评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,308评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,538评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,338评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,260评论 2 352