一、獲取url
打開中國教育在線網(wǎng),按 F12
,頂部選擇NetWork
,選擇XHR
刷新頁面,觀察url
,通過對Reponse
的分析找到真正的url
為:https://api.eol.cn/gkcx/api/
數(shù)據(jù)存儲在Json
中。
再點擊Headers
,查看請求參數(shù)
請求方式為POST
二、發(fā)送請求
拿到url,我們就可以利用requests
模擬瀏覽器發(fā)送請求,拿到返回的Json
數(shù)據(jù)。代碼如下:
# 導入包
import numpy as np
import pandas as pd
import requests
import json
from fake_useragent import UserAgent
import time
# 獲取一頁
def get_one_page(page_num):
# 獲取URL
url = 'https://api.eol.cn/gkcx/api/'
# 構造headers
headers = {
'User-Agent': UserAgent().random,
'Origin': 'https://gkcx.eol.cn',
'Referer': 'https://gkcx.eol.cn/school/search?province=schoolflag=recomschprop=',
}
# 構造data
data = {
'access_token': "",
'admissions': "",
'central': "",
'department': "",
'dual_class': "",
'f211': "",
'f985': "",
'is_dual_class': "",
'keyword': "",
'page': page_num,
'province_id': "",
'request_type': 1,
'school_type': "",
'size': 20,
'sort': "view_total",
'type': "",
'uri': "apigkcx/api/school/hotlists",
}
# 發(fā)起請求
try:
response = requests.post(url=url, data=data, headers=headers)
except Exception as e:
print(e)
time.sleep(3)
response = requests.post(url=url, data=data, headers=headers)
三、解析json數(shù)據(jù)
根據(jù)Response
返回的Json
格式,解析出我們想要的內容,代碼如下:
# 解析獲取數(shù)據(jù)
school_data = json.loads(response.text)['data']['item']
# 學校名
school_name = [i.get('name') for i in school_data]
# 隸屬部門
belong = [i.get('belong') for i in school_data]
# 高校層次
dual_class_name = [i.get('dual_class_name') for i in school_data]
# 是否985
f985 = [i.get('f985') for i in school_data]
# 是否211
f211 = [i.get('f211') for i in school_data]
# 辦學類型
level_name = [i.get('level_name') for i in school_data]
# 院校類型
type_name = [i.get('type_name') for i in school_data]
# 是否公辦
nature_name = [i.get('nature_name') for i in school_data]
# 人氣值
view_total = [i.get('view_total') for i in school_data]
# 省份
province_name = [i.get('province_name') for i in school_data]
# 城市
city_name = [i.get('city_name') for i in school_data]
# 區(qū)域
county_name = [i.get('county_name') for i in school_data]
# 保存數(shù)據(jù)
df_one = pd.DataFrame({
'school_name': school_name,
'belong': belong,
'dual_class_name': dual_class_name,
'f985': f985,
'f211': f211,
'level_name': level_name,
'type_name': type_name,
'nature_name': nature_name,
'view_total': view_total,
'province_name': province_name,
'city_name': city_name,
'county_name': county_name,
})
return df_one
四、存入Excel
先將數(shù)據(jù)存入Pandas
,用于做數(shù)據(jù)分析,再寫入Excel
存儲。
# 獲取多頁
def get_all_page(all_page_num):
# 存儲表
df_all = pd.DataFrame()
# 循環(huán)頁數(shù)
for i in range(all_page_num):
# 打印進度
print(f'正在獲取第{i + 1}頁的高校信息')
# 調用函數(shù)
df_one = get_one_page(page_num=i+1)
# 追加
df_all = df_all.append(df_one, ignore_index=True)
# 休眠
time.sleep(np.random.uniform(2))
return df_all
# 運行函數(shù)
df_school = get_all_page(all_page_num=143)
# 讀出數(shù)據(jù)
df_school.to_excel('./data/全國高校數(shù)據(jù).xlsx', index=False)
五、運行代碼
六、數(shù)據(jù)展示
七、數(shù)據(jù)可視化
1.各省市地區(qū)高校數(shù)量分布 柱形圖:
地圖
各個省的高校層次分布
全國高校類型分布
有了上面的數(shù)據(jù),是不是對全國的高校有一定了解了
到此這篇關于用Python爬取各大高校并可視化幫弟弟選大學,弟弟直呼牛X的文章就介紹到這了,更多相關Python爬取數(shù)據(jù)并可視化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python爬蟲實戰(zhàn)之爬取京東商品數(shù)據(jù)并實實現(xiàn)數(shù)據(jù)可視化
- Python爬取股票信息,并可視化數(shù)據(jù)的示例
- Python爬取數(shù)據(jù)并實現(xiàn)可視化代碼解析
- python如何爬取網(wǎng)站數(shù)據(jù)并進行數(shù)據(jù)可視化