欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

爬取肯德基餐厅查询

程序员文章站 2022-05-02 20:48:03
...

1 需求

爬取肯德基餐厅查询指定地点的餐厅数据,并实现持续化存储。

2 代码实现

import requests


url = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'}
place = input("Please enter a place: ")
data = {
    'op': "keyword",
    'cname': '',
    'pid': '',
    'keyword': place,
    'pageSize': '10',
}
response = requests.post(url=url, data=data, headers=headers)
page_text = response.text
fileName = 'KFC.text'
with open(fileName, 'w', encoding='utf-8') as fp:
    fp.write(page_text)
print("OVER!")