爬虫练习分享--爬取肯德基地址
import requestsimport json
class EasyKFC:
URL = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname"
# 得到所在城市kfc门店的数量 params:int
@staticmethod
def get_num(city):
"""
:param city:str
:return:int
"""
params = {
'cname': city,
'pageSize': 0
}
html = requests.post(EasyKFC.URL, data=params)
info = json.loads(html.text)
return int(info['Table'][0]['rowcount'])
# 得到所有kfc门店
@staticmethod
def get_all_info(city):
"""
:param city:str
:return:dict
"""
params = {
'cname': city,
'pageSize': EasyKFC.get_num(city)
}
html = requests.post(EasyKFC.URL, data=params)
info = json.loads(html.text)
info['rowcount'] = int(info['Table'][0]['rowcount'])
for i in info['Table1']:
i['addressDetail'] = i['cityName'] + i['addressDetail']
i['storeName'] = i['storeName'] + '餐厅'
info.pop('Table')
return info
# 指定所在城市kfc最大获取数量 返回一个字典,
@staticmethod
def get_info(city, num):
"""
:param city:str
:param num:int
:return:dict
"""
params = {
'cname': city,
'pageSize': num
}
html = requests.post(EasyKFC.URL, data=params)
info = json.loads(html.text)
info['rowcount'] = int(info['Table'][0]['rowcount'])
for i in info['Table1']:
i['addressDetail'] = i['cityName'] + i['addressDetail']
i['storeName'] = i['storeName'] + '餐厅'
info.pop('Table')
return info
本帖最后由 yagiza 于 2020-8-13 11:27 编辑
看的头疼,帮你代码写入
import requests
import json
class EasyKFC:
URL = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname"
# 得到所在城市kfc门店的数量 params:int
@staticmethod
def get_num(city):
"""
:param city:str
:return:int
"""
params = {
'cname': city,
'pageSize': 0
}
html = requests.post(EasyKFC.URL, data=params)
info = json.loads(html.text)
return int(info['Table']['rowcount'])
# 得到所有kfc门店
@staticmethod
def get_all_info(city):
"""
:param city:str
:return:dict
"""
params = {
'cname': city,
'pageSize': EasyKFC.get_num(city)
}
html = requests.post(EasyKFC.URL, data=params)
info = json.loads(html.text)
info['rowcount'] = int(info['Table']['rowcount'])
for i in info['Table1']:
i['addressDetail'] = i['cityName'] + i['addressDetail']
i['storeName'] = i['storeName'] + '餐厅'
info.pop('Table')
return info
# 指定所在城市kfc最大获取数量 返回一个字典,
@staticmethod
def get_info(city, num):
"""
:param city:str
:param num:int
:return:dict
"""
params = {
'cname': city,
'pageSize': num
}
html = requests.post(EasyKFC.URL, data=params)
info = json.loads(html.text)
info['rowcount'] = int(info['Table']['rowcount'])
for i in info['Table1']:
i['addressDetail'] = i['cityName'] + i['addressDetail']
i['storeName'] = i['storeName'] + '餐厅'
info.pop('Table')
return info 请问爬来的数据是啥格式的,是txt还是html还是json,我想学习一下爬取Oxford English Dictionary ,不知道能不能爬取 好东西,谢谢分享 很想学习学习 heihei1314 发表于 2020-8-13 10:55
爬虫爬的好,牢饭吃到老。。。
哈哈哈哈哈哈,妙啊 不错!小试牛刀🐮 活到老,学到了。受教{:1_893:} 这是2还是3?
页:
[1]
2