数据爬取——北京肯德基门店信息

import requests, json

url = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36"
}
addres = input("请输入查询的城市>>>")
for page in range(1, 10):
    data = {
        "cname": addres,
        "pid": "",
        "pageIndex": str(page),
        "pageSize": "10",
    }

    res = requests.post(url=url, headers=headers, data=data).json()
    data_detali = res.get("Table1")

    content = {"店名":None,"地址":None}

    for store in data_detali:
        storename = store.get("storeName")           # 获取店名
        storeaddress = store.get("addressDetail")    # 获取店地址
        content["店名"] = storename
        content["地址"] = storeaddress
        # print(content)

        with open("kfc.txt", "a", encoding="utf8") as fp:  # 存储数据
            json.dump(content, fp)

 

你可能感兴趣的:(数据分析)