该怎么引用json里的数据啊,什么格式呀
import requestsimport json
location=input('输入你想要查询的地名:')
url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
param={'cname': '','pid':'', 'keyword':location,'pageIndex':'1','pageSize': '10'}
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 SLBrowser/8.0.1.4031 SLBChan/105'}
response=requests.post(url=url,data=param,headers=headers)
list=response.json()
numbers=int(list)
pages=numbers/10
for page in range(1,pages+1):
param = {'cname': '', 'pid': '', 'keyword': location, 'pageIndex': page, 'pageSize': '10'}
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 SLBrowser/8.0.1.4031 SLBChan/105'}
response = requests.post(url=url, data=param, headers=headers)
list = response.json()
for i in list['Table1']:
print(i['cityName']+i['storeName'],i['addressDetail'])
你可以使用 response.json() 方法将响应的JSON数据转换为Python字典或列表。在你的代码中,你已经正确地使用了 response.json() 来获取JSON数据并将其赋值给变量 list 。
然后,你可以根据JSON的结构来引用数据。在你的代码中,你可以使用 list['Table1'] 来获取名为"Table1"的键对应的值,它是一个包含店铺信息的列表。然后,你可以使用索引来访问列表中的每个店铺信息。
例如,你可以使用 i['cityName'] 来获取每个店铺的城市名,使用 i['storeName'] 来获取每个店铺的名称,使用 i['addressDetail'] 来获取每个店铺的详细地址。
所以,你可以将最后的代码修改为:
for i in list['Table1']:
print(i['cityName'] + i['storeName'], i['addressDetail'])
这将打印出每个店铺的城市名、店铺名和详细地址。
页:
[1]