爬取的数据是没有问题的,就是数据导入的结果出现了问题,我也知道是语法的不对,不知道咋改,导入excel使用的模块是openxlpy,这个问题困扰好长时间,求指教,源代码如下:
```python
import requests
from jsonpath import jsonpath
import json,xlwt,openpyxl
def requests_data(page):
url='https://www.codemart.com/api/project?page={}'.format(page)
headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36','accept': 'application/json'}
response=requests.get(url,headers=headers)
response.encoding = response.apparent_encoding
data=json.loads(response.text)
return data
def clean_data():
get_data=requests_data(1)
maxpage=jsonpath(get_data,'$..totalPage')[0] #获取最大页数
return maxpage
def send_data():
maxpage=clean_data()
for i in range(1,maxpage+1): #遍历每一页
final_data=requests_data(i) #获取该页总数据
# project_name=jsonpath(final_data,'$..name') #在该页数据中提取处项目名称
project_name=final_data['rewards'] #获取该页数据中目标数据
list=[]
for p in project_name:
name = p['name']
money = p['price']
roles = p['roles']
type = p['typeText']
# list.append([name,roles,type,money])
list = [name, roles, type, money]
print(list)
save(list)
def save(list):
wb = openpyxl.Workbook() # 创建一个工作簿
sheet = wb.active # 创建一个活动表
list3 = ['项目', '招募', '类型', '金额']
sheet.append(list3)
sheet.append(list)
wb.save('data.xlsx')
if __name__=='__main__':
send_data()
最终的excel结果是这样的
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/600948720926127.png 'image.png')