import requests
def inte(kw, i):
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
param = {
'cname': '',
'pid': '',
'keyword': kw,
'pageIndex': i,
'pageSize': '10'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62'}
response = requests.get(url=url, params=param, headers=headers)
page_text = response.text # 将源码变为文本形式
# print(page_text)
# print(type(page_text))
return(page_text)
if __name__=='__main__':
kw = input('请输入你想查询的地点:')
filename = kw+'.txt'
with open(filename, 'a+', encoding='utf-8') as fp:
for i in range(1,20):
map_text = inte(kw, i)
# map_values = map_text.values() # 本来以为出来的是字典类型,{{}}把我骗了md,这是str
if '[]' in map_text:
break
else :
fp.write("%s\n" % map_text)
print("over!")
很坑,上回竟然审核未通过,这回不多说别的,直接上代码。记录初学爬虫
|