python爬虫——Ajax-post爬取肯德基官网

1.Ajax-post方法爬取肯德基官网

    ~~~     1.1准备工作

点击最下方的餐厅查询

随便查询一个城市,下方红色方框圈起来的就是Ajax
python爬虫——Ajax-post爬取肯德基官网_第1张图片
python爬虫——Ajax-post爬取肯德基官网_第2张图片
python爬虫——Ajax-post爬取肯德基官网_第3张图片
python爬虫——Ajax-post爬取肯德基官网_第4张图片
门店信息:
在这里插入图片描述

    ~~~     1.2编写代码

import urllib.request
import urllib.parse

post_url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
city=input('请输入要查询的城市:')
page=input('请输入要查询第几页:')
size=input('请输入每页要查询多少条信息:')
form_data={
    'cname': city,
    'pid':' ',
    'pageIndex': page,
    'pageSize':size,
}
# 伪装头部
headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
}
# 转换数据
form_data=urllib.parse.urlencode(form_data).encode()
# 构建请求
request=urllib.request.Request(url=post_url,headers=headers)
# 发送请求
response=urllib.request.urlopen(request,form_data)
print(response.read().decode())

python爬虫——Ajax-post爬取肯德基官网_第5张图片

{"Table":[{"rowcount":431}],"Table1":[{"rownum":6,"storeName":"航天桥","addressDetail":"阜成路51-2","pro":"24小时,店内参观","provinceName":"北京市","cityName":"北京市"},{"rownum":7,"storeName":"展览路(德宝)","addressDetail":"西外大街德宝新园14号","pro":"Wi-Fi,店内参观,礼品卡","provinceName":"北京市","cityName":"北京市"},{"rownum":8,"storeName":"劲松","addressDetail":"劲松4区401楼","pro":"24小时,Wi-Fi,店内参观,礼品卡,生日餐会","provinceName":"北京市","cityName":"北京市"},{"rownum":9,"storeName":"西罗园","addressDetail":"西罗园4区南二段","pro":"Wi-Fi,店内参观,礼品卡,生日餐会","provinceName":"北京市","cityName":"北京市"},{"rownum":10,"storeName":"蓝桥","addressDetail":"蓝桥餐厅工体北路11-1号","pro":"24小时,Wi-Fi,点唱机,礼品卡","provinceName":"北京市","cityName":"北京市"}]}

你可能感兴趣的:(python爬虫)