简易分析--高德poi
一.获取数据
参考:https://lbs.amap.com/api/webservice/guide/api/search/
1.第一步获取key
点击右上角的控制台
点击我的应用,并点击右上角的创建应用,就可以获取key
注意有使用次数的限制
2.简单调用
具体其他参数参考:https://lbs.amap.com/api/webservice/guide/api/search/
url ='https://restapi.amap.com/v3/place/text?types=<类型编号>&city=<城市编号>&offset=20&page=1&key=<用户的key>'
response = requests.get(url)
response.text
我测试的是广州(440100)的餐饮服务(050000),返回的是json格式的数据
利用pprint更加清晰打印出json数据
# print(type(response.json()))
# pprint.pprint(response.json())
#效果一样
result = json.loads(response.text)
# print(type(result))
pprint.pprint(result)
3.获取需要的数据
pois = result['pois']
for poi in pois:
name = poi['name']
types = poi['type']
type1 = types.split(';')[0]
type2 = types.split(';')[1]
type3 = types.split(';')[2]
location = poi['location']
lon = location.split(',')[0]
lat = location.split(',')[1]
print(name,type1,type2,type3,lon,lat)
4.将爬取的数据放入csv文件中
获取多页的数据,然后保存到csv中
urls = ['https://restapi.amap.com/v3/place/text?types=050000&city=440100&offset=20&page={}&key=自己的key'.format(str(i)) for i in range(1,100)]
headers = ['店名','类型1','类型2','类型3','经度','纬度']
def getJson(url):
response = requests.get(url)
return response.text
def parse(text):
result = json.loads(text)
count = result['count']
if count == '0': #直到有一页已经没有数据,就break
break
pois = result['pois']
for poi in pois:
name = poi['name']
types = poi['type']
type1 = types.split(';')[0]
type2 = types.split(';')[1]
type3 = types.split(';')[2]
location = poi['location']
lon = location.split(',')[0]
lat = location.split(',')[1]
print(name,type1,type2,type3,lon,lat)
f_csv.writerow([name,type1,type2,type3,lon,lat])
#餐饮
with open('data.csv','a+',newline='',encoding='utf-8') as f:
f_csv = csv.writer(f)
f_csv.writerow(headers)
for url in urls:
text = getJson(url)
parse(text)
二.poi分析
1.获取数据(经纬度写反了。。。)
data = pd.read_csv('data.csv',encoding='utf-8')
data.head()
2.对类型进行分析
type_demo = data['类型1'].value_counts()
type_demo
过滤掉次数少于10的
temp = type_demo[type_demo < 10]
temp
data.replace(temp.index, np.nan, inplace=True)
type_1 = data.groupby('类型1')['类型1'].count()
type_1
画饼图
from pyecharts import options as opts
from pyecharts.charts import Pie
c = (
Pie()
.add(
"",
[list(z) for z in zip(type_1.index, type_1)],
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-type1"))
.render_notebook()
)
c
type_2 = data['类型2'].value_counts()
type_2
选取前十画图
from pyecharts import options as opts
from pyecharts.charts import Bar
#可能要先转list
attr = list(type_2.index)[0:10]
v = list(type_2)[0:10]
#注意设置间隔和标签对齐
c = (
Bar()
.add_xaxis(attr )
.add_yaxis("type2", v)
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-type2", subtitle=""),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(interval=0, rotate = "0"),
axistick_opts=opts.AxisTickOpts(is_align_with_label=True)))
.render_notebook()
)
c
3.利用经纬度绘制热力图(利用folium)
from folium import plugins
# from folium.plugins import HeatMap
heatmap1 = folium.Map(location=[23.20,113.30],zoom_start=11)
heatmap1.add_child(plugins.HeatMap([row['纬度'],row['经度']] for name,row in data.iterrows()))
heatmap1
# m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)