# 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