Python3 爬取全国高校排名信息

Python3 爬取全国高校排名信息

实现源码

copy 后可直接使用

import requests
import json
import csv


# 构建请求头
headers = {
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'contentType': 'application/x-www-form-urlencoded; charset=utf-8',
    'Cookie': 'cfm-major=true',
    'Host': 'gaokao.afanti100.com',
    'media': 'PC',
    'Referer': 'http://gaokao.afanti100.com/university.html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest',
}


# 声明一个列表存储字典
data_list = []


def get_index():

    page = 1
    while True:
        if page > 188:
            break
        url = 'http://gaokao.afanti100.com/api/v1/universities/?degree_level=0&directed_by=0' \
              '&university_type=0&location_province=0&speciality=0&page={}'.format(page)
        # page自增一实现翻页
        page += 1
        # 请求url并返回的是json格式
        resp = requests.get(url, headers=headers).json()
        # 取出大学所在的键值对
        university_lsts = resp.get('data').get('university_lst')
        if university_lsts:
            get_info(university_lsts)
        else:
            continue


def get_info(university_lsts):
    # 判断列表是否不为空
    if university_lsts:
        # 遍历列表取出每个大学的信息
        for university_lst in university_lsts:
            # 声明一个字典存储数据
            data_dict = {}

            # 大学名字
            data_dict['name'] = university_lst.get('name')
            # 大学排名
            data_dict['ranking'] = university_lst.get('ranking')
            # 大学标签
            data_dict['tag_lst'] = university_lst.get('tag_lst')
            # 大学重点学科
            data_dict['key_major_count'] = university_lst.get('key_major_count')
            # 硕士点数
            data_dict['graduate_program_count'] = university_lst.get('graduate_program_count')
            # 博士点数
            data_dict['doctoral_program_count'] = university_lst.get('doctoral_program_count')
            # 是否211
            data_dict['is_211'] = university_lst.get('is_211')
            # 是否985
            data_dict['is_985'] = university_lst.get('is_985')
            # 哪个省
            data_dict['location_province'] = university_lst.get('location_province')
            # 哪个城市
            data_dict['location_city'] = university_lst.get('location_city')
            # 大学类型
            data_dict['university_type'] = university_lst.get('university_type')
            # logo
            data_dict['logo_url'] = university_lst.get('logo_url')

            data_list.append(data_dict)
            print(data_dict)


def save_file():
    # 将数据存储为json文件
    with open('大学排名信息.json', 'w', encoding='utf-8') as f:
        json.dump(data_list, f, ensure_ascii=False, indent=4)
    print('json文件保存成功')

    # 将数据存储为csv文件
    # 表头
    title = data_list[0].keys()
    with open('大学排名信息.csv', 'w', encoding='utf-8', newline='') as f:
        writer = csv.DictWriter(f, title)
        # 写入表头
        writer.writeheader()
        # 写入数据
        writer.writerows(data_list)
    print('csv文件保存成功')


def main():
    get_index()
    save_file()


if __name__ == '__main__':
    main()

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名技术极客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值