# coding=utf-8
import json
import pandas as pd
import requests
def detail(page_num):
heads = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36'} # 請求頭
url = 'https://static-data.gaokao.cn/www/2.0/special/%s/pc_special_detail.json'#Url
d2 = pd.DataFrame()
#分頁爬取一頁10個,需要對pandas進行安裝pip install openpyxl
for i in range(1,page_num):
response = requests.get(url % (i), headers=heads)
if response!=None:
json_data = json.loads(response.text)
my_json = json_data['data'] # 獲得josn 數據的根目錄
df3 = pd.DataFrame({#d對my_json中文件進行獲取
'id':my_json['id'],
'name':my_json['name'],
'內容':my_json['content'],
'工作':my_json['job'],
'code':my_json['code'],
'degree':my_json['degree'],
'年限':my_json['limit_year'],
'男女比例':my_json['rate'],
'type':my_json['type'],
'type_detail':my_json['type_detail']
}, index=[0])
d2 = d2.append(df3, ignore_index=True)
print(d2)
d2.to_excel("major.xlsx", index=False)
detail(5)
————————————————
版權聲明:本文為CSDN博主「螺旋大西瓜」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_45208256/article/details/124950788