import requests
import json
import openpyxl
def open_url(url):
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
global kw
kw=input('输入查询地区:')
list1=[]
list2=[]
list3=[]
for i in range(1,5):
data={"cname":"",
"pid":"",
"keyword": "%s"%(kw),
"pageIndex": i,
"pageSize": "10",
}
response=requests.post(url,data=data,headers=headers)
req=json.loads(response.text)['Table1']
for each in req:
list1.append(each['storeName'])
list2.append(each['addressDetail'])
list3.append(each["provinceName"]+each['cityName'])
result=[]
for i in range(len(list1)):
result.append([list1[i],list2[i],list3[i]])
wb=openpyxl.Workbook()
wa=wb.active
wa['A1']='店名'
wa['B1']='地址'
wa['C1']='位置'
wa.column_dimensions['A'].width = 30
wa.column_dimensions['B'].width = 45
wa.column_dimensions['C'].width = 30
for each in result:
wa.append(each)
wb.save('%s.xlsx'%(kw))
if __name__=='__main__':
url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
open_url(url)
|