KFC-位置分页爬虫

import requests
import json

if name == 'main':
#记录总数
raw_count=0
#查询页数
page=1
#存储数据
data=[]
#判断有无数据条件
size=10
#python中没有null,防止转dict异常
global null
null = ''
place=input("请输入想要查询的区域:")
while True:
url="http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword"
params={
'cname':'',
'pid':'',
'keyword':place,
'pageIndex':page,
'pageSize':10
}
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
res=requests.post(url=url,params=params,headers=headers)
print(eval(res.text))
movie_dict=eval(res.text)
raw_count=movie_dict['Table'][0]['rowcount']
if raw_count>size:
data.extend(movie_dict['Table1'])
else:
data.extend(movie_dict['Table1'])
with open(f"./{place}.json","w",encoding="utf8")as fw:
json.dump(data,fw,ensure_ascii=False)
break
size+=10
page+=1

posted @ 2019-08-21 17:04  zx125  阅读(173)  评论(0编辑  收藏  举报