"""
获取KFC餐厅:http://www.kfc.com.cn/kfccda/index.aspx
代码仅供学习
date:2020-05-23
"""
import requests
import json
if __name__ == '__main__':
keyword = input('查询城市:')
pageSize = input('一页获取几条:')
# 1.指定URL
url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
# 2.UA伪装
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36'
}
# 3.post数据
data = {
'cname': '',
'pid': '',
'keyword': keyword,
'pageIndex': '1',
'pageSize': pageSize
}
# 4.发起POST请求
resp = requests.post(url=url, headers=headers, data=data)
# 5.获取数据,把text转为字典
page_dic = json.loads(resp.text)
# print(page_dic)
# 6.解析数据
rowcount = page_dic['Table'][0]['rowcount']
print('总共{}家'.format(rowcount))
for i in page_dic['Table1']:
print(i)
爬虫学习之02-request模块爬取KFC餐厅
最新推荐文章于 2024-02-13 20:22:29 发布