代码
import json
import csv
# 打开JSON文件并加载数据
with open('4_.json', 'rb') as f:
data_list = json.load(f)
arr = data_list['data']
print(arr['list'])
for data in arr:
# 提取停车场信息
mchname = data['list']['mchname']
parkname = data['list']['parkname']
totalspace = data['list']['totalspace']
usedspace = data['list']['usedspace']
freeMinute = data['list']['freeMinute']
# 将信息写入CSV文件
with open('parking_info2.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['运营公司', '停车场名称', '总车位数', '当前使用车位数','免费时长'])
writer.writerow([mchname, parkname, totalspace, usedspace,freeMinute])
报错信息
Traceback (most recent call last):
File "F:\text1\爬虫\pc4.py", line 11, in <module>
mchname = data['list']['mchname']
TypeError: string indices must be integers
json文件
{
"status":1,
"msg":"success",
"data":{
"total":153,
"offset":0,
"limit":0,
"list":[
{
"id":173,
"parkid":"1-01-2-3019",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"测绘路路北",
"address":"测绘路路北",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"测绘路",
"lng":108.96202,
"lat":34.238243,
"img":null,
"totalspace":32,
"usedspace":22,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.5372,
"unuse":10,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":4,
"parkArea":2
},
{
"id":96,
"parkid":"1-01-2-4025",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"皇甫路(兴庆路-交大街段)双侧",
"address":"皇甫路(兴庆路-交大街段)双侧",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"皇甫路",
"lng":108.99051,
"lat":34.24799,
"img":null,
"totalspace":92,
"usedspace":31,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.5374,
"unuse":61,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":5,
"parkArea":2
},
{
"id":68,
"parkid":"1-01-2-3031",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"测绘东路(测绘路至友谊东路段)双侧",
"address":"测绘东路(测绘路至友谊东路段)双侧",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"测绘东路",
"lng":108.960236,
"lat":34.237778,
"img":null,
"totalspace":49,
"usedspace":32,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.5521,
"unuse":17,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":4,
"parkArea":2
},
{
"id":69,
"parkid":"1-01-2-3032",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"测绘东路(建设路-测绘路段)双侧",
"address":"测绘东路(建设路-测绘路段)双侧",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"测绘东路",
"lng":108.960236,
"lat":34.237778,
"img":null,
"totalspace":62,
"usedspace":54,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.5521,
"unuse":8,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":4,
"parkArea":2
},
{
"id":61,
"parkid":"1-01-2-3021",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"草场坡(南二环-永宁路段)双侧",
"address":"草场坡(南二环-永宁路段)双侧",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"草场坡",
"lng":108.94981,
"lat":34.23448,
"img":null,
"totalspace":142,
"usedspace":130,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.5819,
"unuse":12,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":3,
"parkArea":2
},
{
"id":62,
"parkid":"1-01-2-3022",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"草场坡(长安北路-永宁路段)双侧",
"address":"草场坡(长安北路-永宁路段)双侧",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"草场坡",
"lng":108.94981,
"lat":34.23448,
"img":null,
"totalspace":30,
"usedspace":30,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.5819,
"unuse":0,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":3,
"parkArea":2
},
{
"id":137,
"parkid":"2-01-2-3024",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"友谊东路路南(西安市卫生学校家属院前)",
"address":"友谊东路路南(西安市卫生学校家属院前)",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"友谊东路",
"lng":108.96803,
"lat":34.241184,
"img":null,
"totalspace":17,
"usedspace":14,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.6275,
"unuse":3,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":3,
"parkArea":2
},
{
"id":139,
"parkid":"2-01-2-4009",
"parentid":null,
"mchid":"161",
"mchname":"碑林区运营公司",
"parkname":"友谊东路路南(铁安一街-铁安二街段)",
"address":"友谊东路路南(铁安一街-铁安二街段)",
"citycode":"西安市",
"areacode":"碑林区",
"streetcode":"友谊东路",
"lng":108.96803,
"lat":34.241184,
"img":null,
"totalspace":16,
"usedspace":14,
"price":2,
"parktype":1,
"ischarge":false,
"isbook":false,
"isused":true,
"servicescope":null,
"text":null,
"maxPriceOfDay":null,
"freeMinute":15,
"dist":1394.6275,
"unuse":2,
"isstore":null,
"paytypelist":null,
"usableBookingCarportNumber":null,
"monthvipid":null,
"pricestrategyid":5,
"pricenewenergyid":9,
"innerpricestrategyid":null,
"strobeflag":null,
"businesspricestrategyid":null,
"pricepublicityid":7,
"pricePublicityFrontList":null,
"aliParkingid":"",
"carPortBOS":null,
"outerinfo":false,
"limitType":null,
"limitDate":null,
"limitTime":null,
"limitWorkday":null,
"bookingRule":"",
"bookingTimeFrameBOS":null,
"nowtime":null,
"bookingTimeRule":false,
"districtid":4,
"parkArea":2
},
],
"searchCount":true
}
}
问题报错怎么解决