先看浏览器中的网络请求:附上源码:
# http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname import urllib.request import urllib.parse def down_load(page): url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname' headers = { 'Cookie':'ARRAffinity=6385654a9a132fb312f7e965655660e2723317ef36750cdf9509cabb155e9905; ARRAffinitySameSite=6385654a9a132fb312f7e965655660e2723317ef36750cdf9509cabb155e9905; _ga=GA1.3.1156258771.1645768700; _gid=GA1.3.1891586264.1645768700; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2217f2f75999f128-0d7ab215fc8057-a3e3164-1327104-17f2f7599a0dc7%22%2C%22%24device_id%22%3A%2217f2f75999f128-0d7ab215fc8057-a3e3164-1327104-17f2f7599a0dc7%22%2C%22props%22%3A%7B%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%2C%22%24latest_referrer_host%22%3A%22www.baidu.com%22%2C%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%7D%7D; Adshow=1; _gat=1' , 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36' } data ={ 'cname': '重庆' , 'pageIndex': page , 'pageSize': '10' } data = urllib.parse.urlencode(data).encode('utf-8') # 定制请求对象 request = urllib.request.Request(url = url ,data = data, headers = headers ) # 模拟浏览器向服务器发送请求 response = urllib.request.urlopen(request) # 返回响应数据 content = response.read().decode('utf-8') # 将数据下载到本地 with open('location' + str(page) +'.json' , 'w',encoding='utf-8') as fp: fp.write(content) if __name__ == '__main__': #程序入口 start_page = int(input('请输入起始页:')) end_page = int(input('请输入结束页:')) for page in range(start_page,end_page+1): down_load(page) # 调用函数 print('Finally')