考研择校神器!Python爬取中国大学排名

一、爬取目标

考研择校神器!Python爬取中国大学排名

二、完整源码

#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
#
# Copyright (c) 2024 愤怒的it, All Rights Reserved.
# FileName : code.py
# Date : 2024.02.02
# Author : 愤怒的it
# Version : 1.0.0
# Node : 欢迎关注微信公众号【愤怒的it男】
#
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

import csv
import js2py
import requests
from tabulate import tabulate

def getData(url, headers):
    response = requests.get(url=url, headers=headers)
    jsonp_output = '''   
    var result
    function __NUXT_JSONP__(route, data) {
        result = data.data[0].univData;
    }
    '''

    js_code = response.text + jsonp_output
    js_obj=js2py.EvalJs()
    js_obj.execute(js_code)
    dataList = js_obj.result.to_list()
    
    result = []
    for data in dataList:
        if 'univTags' in data:
            univTags = '-'.join(data['univTags'])
        else:
            univTags = ''
        indData = data['indData']["411"]
        d = [data['ranking'], data['univNameCn'], data['univNameEn'], univTags, data['province'], data['univCategory'], data['score'], indData]
        result.append(d)
    return result

def printData(result):
    headers = ["排名""中文名""英文名""标签""省市""类型""总分""办学层次"]
    table = tabulate(result, headers=headers, maxcolwidths=[NoneNone45NoneNoneNoneNoneNone], tablefmt='grid')
    print(table)

def saveData(result):
    with open('中国大学排名.csv''w', encoding='utf-8', newline=''as file:
        writer = csv.writer(file)
        writer.writerows(result)

def main():
    baseUrl = 'https://www.shanghairanking.cn/_nuxt/static/1706099724/rankings/bcur/2023/payload.js'
    headers = {'User-Agent''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    result = getData(baseUrl, headers)
    printData(result)
    saveData(result)
    
if __name__== "__main__" :
    main()

三、爬取结果

+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 排名 | 中文名 | 英文名 | 标签 | 省市 | 类型 | 总分 | 办学层次 |
+========+============================+===============================================+================+========+========+========+============+
|      1 | 清华大学 | Tsinghua University | 双一流-985-211 | 北京 | 综合1004.1 | 37.5       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      2 | 北京大学 | Peking University | 双一流-985-211 | 北京 | 综合 |  910.5 | 35.0       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      3 | 浙江大学 | Zhejiang University | 双一流-985-211 | 浙江 | 综合 |  822.9 | 35.9       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      4 | 上海交通大学 | Shanghai Jiao Tong University | 双一流-985-211 | 上海 | 综合 |  778.6 | 35.7       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      5 | 复旦大学 | Fudan University | 双一流-985-211 | 上海 | 综合 |  712.4 | 36.5       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      6 | 南京大学 | Nanjing University | 双一流-985-211 | 江苏 | 综合 |  676.2 | 37.7       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      7 | 中国科学技术大学 | University of Science and Technology of China | 双一流-985-211 | 安徽 | 理工 |  608.6 | 40.0       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      8 | 华中科技大学 | Huazhong University of Science and Technology | 双一流-985-211 | 湖北 | 综合 |  606.2 | 32.2       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|      9 | 武汉大学 | Wuhan University | 双一流-985-211 | 湖北 | 综合 |  599.1 | 31.8       |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
|     10 | 西安交通大学 | Xi'an Jiaotong University | 双一流-985-211 | 陕西 | 综合 | 572.6 | 34.2 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 11 | 中山大学 | Sun Yat-Sen University | 双一流-985-211 | 广东 | 综合 | 570 | 31.1 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 12 | 四川大学 | Sichuan University | 双一流-985-211 | 四川 | 综合 | 546.9 | 31.5 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 13 | 哈尔滨工业大学 | Harbin Institute of Technology | 双一流-985-211 | 黑龙江 | 理工 | 544.2 | 32.7 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 14 | 北京航空航天大学 | Beihang University | 双一流-985-211 | 北京 | 理工 | 543.5 | 33.4 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 15 | 东南大学 | Southeast University | 双一流-985-211 | 江苏 | 综合 | 532.3 | 35.4 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 16 | 北京理工大学 | Beijing Institute of Technology | 双一流-985-211 | 北京 | 理工 | 529.3 | 33.4 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 17 | 同济大学 | Tongji University | 双一流-985-211 | 上海 | 综合 | 527 | 33.2 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 18 | 中国人民大学 | Renmin University of China | 双一流-985-211 | 北京 | 综合 | 526.3 | 34.7 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 19 | 北京师范大学 | Beijing Normal University | 双一流-985-211 | 北京 | 师范 | 523.4 | 33.0 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 20 | 南开大学 | Nankai University | 双一流-985-211 | 天津 | 综合 | 490.4 | 31.7 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 21 | 天津大学 | Tianjin University | 双一流-985-211 | 天津 | 理工 | 480.5 | 33.0 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 22 | 山东大学 | Shandong University | 双一流-985-211 | 山东 | 综合 | 472.8 | 29.7 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 23 | 中南大学 | Central South University | 双一流-985-211 | 湖南 | 综合 | 466.1 | 30.7 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 24 | 厦门大学 | Xiamen University | 双一流-985-211 | 福建 | 综合 | 463.2 | 32.5 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 25 | 西北工业大学 | Northwestern Polytechnical University | 双一流-985-211 | 陕西 | 理工 | 462 | 33.4 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 26 | 华南理工大学 | South China University of Technology | 双一流-985-211 | 广东 | 理工 | 444.2 | 30.8 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 27 | 吉林大学 | Jilin University | 双一流-985-211 | 吉林 | 综合 | 439.1 | 30.5 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 28 | 电子科技大学 | University of Electronic Science and | 双一流-985-211 | 四川 | 理工 | 437 | 32.2 |
| | | Technology of China | | | | | |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 29 | 湖南大学 | Hunan University | 双一流-985-211 | 湖南 | 综合 | 435.5 | 31.5 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 30 | 中国农业大学 | China Agricultural University | 双一流-985-211 | 北京 | 农业 | 432.8 | 31.8 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 31 | 华东师范大学 | East China Normal University | 双一流-985-211 | 上海 | 师范 | 428.2 | 32.7 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 32 | 大连理工大学 | Dalian University of Technology | 双一流-985-211 | 辽宁 | 理工 | 417.4 | 31.0 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 33 | 南方科技大学 | Southern University of Science and Technology | 双一流 | 广东 | 综合 | 413 | 31.5 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 34 | 重庆大学 | Chongqing University | 双一流-985-211 | 重庆 | 综合 | 397.7 | 30.3 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 35 | 北京科技大学 | University of Science and Technology Beijing | 双一流-211 | 北京 | 理工 | 382.8 | 33.1 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 36 | 南京理工大学 | Nanjing University of Science and Technology | 双一流-211 | 江苏 | 理工 | 381.2 | 31.2 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 37 | 南京航空航天大学 | Nanjing University of Aeronautics and | 双一流-211 | 江苏 | 理工 | 376.6 | 30.0 |
| | | Astronautics | | | | | |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 38 | 东北大学 | Northeastern University | 双一流-985-211 | 辽宁 | 理工 | 369.4 | 31.2 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 39 | 西安电子科技大学 | Xidian University | 双一流-211 | 陕西 | 理工 | 363.8 | 30.6 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 40 | 兰州大学 | Lanzhou University | 双一流-985-211 | 甘肃 | 综合 | 362.2 | 31.5 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 40 | 苏州大学 | Soochow University (China) | 双一流-211 | 江苏 | 综合 | 362.2 | 29.0 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 42 | 华中农业大学 | Huazhong Agricultural University | 双一流-211 | 湖北 | 农业 | 361 | 29.9 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 43 | 北京交通大学 | Beijing jiaotong University | 双一流-211 | 北京 | 理工 | 346.9 | 31.7 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 44 | 华东理工大学 | East China University of Science and | 双一流-211 | 上海 | 理工 | 346.1 | 30.6 |
| | | Technology | | | | | |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 45 | 上海科技大学 | ShanghaiTech University | 双一流 | 上海 | 综合 | 343.3 | 36.9 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 46 | 郑州大学 | Zhengzhou University | 双一流-211 | 河南 | 综合 | 336.5 | 28.5 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 47 | 华中师范大学 | Central China Normal University | 双一流-211 | 湖北 | 师范 | 334.2 | 30.3 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 48 | 上海大学 | Shanghai University | 双一流-211 | 上海 | 综合 | 333.2 | 32.1 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 49 | 哈尔滨工程大学 | Harbin Engineering University | 双一流-211 | 黑龙江 | 理工 | 332.6 | 31.1 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+
| 50 | 暨南大学 | Jinan University | 双一流-211 | 广东 | 综合 | 332.4 | 28.8 |
+--------+----------------------------+-----------------------------------------------+----------------+--------+--------+--------+------------+

考研择校神器!Python爬取中国大学排名

考研择校神器!Python爬取中国大学排名

四、免责声明

本文所涉及源代码均为个人学习所编写,并于公众号(愤怒的it男)中原创发布,仅可用于计算机技术学习及研究等合法行为,禁止利用本文所涉及源代码从事任何违反本国(地区)法律法规的业务,如有发现存在违法违规行为我会举报到网监部门。

原文始发于微信公众号(愤怒的it男):考研择校神器!Python爬取中国大学排名

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/234381.html

(0)
葫芦侠五楼的头像葫芦侠五楼

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!