# 需求:爬取肯德基的地址
import requests
import json
# step_1 : 指定Url,进行UA伪装
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0'
}
# step_2 :用post方法发送请求
city = input('请您输入一个城市:')
page = input('请您输入页码:')
# 参数
data = {
'name': '',
'pid': '',
'keyword': 'city',
'pageIndex': 'page',
'pageSize': '10',
}
response = requests.post(url=url, data=data, headers=headers)
# step_3 :获得响应数据
dict_obj = response.text
# print(dict_obj)
# step_4 :持久化存储
Filename = city+'肯德基.html'
f1 = open(Filename, 'w', encoding='utf-8')
f1.write(dict_obj)
print(Filename+'位置信息爬取完成!!!')
|