import urllib.request import urllib.parse import json def getKenData(index): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62', } data = { 'cname': '北京', 'pid': '', 'pageIndex': index, 'pageSize': 10, } reqData = urllib.parse.urlencode(data).encode('utf-8') url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname' request = urllib.request.Request(url=url, headers=headers, data=reqData) response = urllib.request.urlopen(request) content = response.read().decode('utf-8') file = open('kdjj' + str(index) + '.json','w',encoding='utf-8') file.write(content) for i in range(1,11): getKenData(i)
标签:index,utf,request,urllib,ajax,肯德基,import,post,data From: https://www.cnblogs.com/sgj191024/p/17738167.html