
- # 爬取沈阳所有肯德基餐厅位置信息
- import requests
- import json
- import re
-
- url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
- headers = {
- 'User-Agent':
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36'
- }
-
- location_list=[]# 空列表存放餐厅位置
- for pageNum in range(1,18):
- data={
- 'cname': '',
- 'pid': '',
- 'keyword': '沈阳',
- 'pageIndex': pageNum,
- 'pageSize': '10',
- }
- resp = requests.post(url=url,headers=headers,data=data)
- page_text = resp.text
-
- with open ('D:\\Programming\\Microsoft VS Code Data\\WebCrawler\\data\\沈阳kfc位置信息\\'+str(pageNum)+'.json',
- 'a+',
- encoding='utf-8',
- ) as fp:
- json.dump(obj=page_text,fp=fp,ensure_ascii=False)# 将对象序列化为 json 格式并写入文件
- print(pageNum,'页下载成功!')
-
- ex = '\\"addressDetail\\":\\"([^\\\"]*)\\"'# 正则表达式匹配餐厅位置
- page_location = re.findall(ex, page_text, re.S)# 得到每一页的餐厅位置
- location_list += page_location# 将每一页的餐厅位置加入列表
- print(location_list)
-
-
-

