进入肯德基官网
代码编写
import requests
import jsonif __name__ == '__main__':get_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'}key_word = input('enter the place:')data = {'cname':'','pid':'','keyword':key_word,'pagelndex':'1','pageSize':'20'}response = requests.post(url=get_url,data=data,headers=headers)# json 返回的是一个对象(如果确认响应数据是json类型的才可以使用)dic_obj = response.json()file_name = key_word + '.json'fp = open(file_name,'w',encoding='utf-8')json.dump(dic_obj,fp=fp,ensure_ascii=False)
运行结果