import requests
from bs4 import BeautifulSoup
import json
import pandas as pd
from pandas.io.json import json_normalize
data = {
'ins_lat':'37.56682', # 지정한 위도와 경도에서 가까운 순으로 나열
'ins_lng':'126.97865',
'p_sido_cd':'01', # 01=서울시, 08=경기 ... 16=제주
'p_gugun_cd':'', # 세부지역 (지정하지 않으면 시/도 전체)
'in_biz_cd':'',
'set_date':'',
'iend':'1000',
}
url = 'https://www.istarbucks.co.kr/store/getStore.do'
r = requests.post(url, data=data)
r.text[:1000] # 수신된 데이터의 앞부분만 확인
'{"list":[{"seq":0,"sido_cd":null,"sido_nm":null,"gugun_cd":null,"gugun_nm":null,"code_order":null,"view_yn":null,"store_num":null,"sido":null,"gugun":null,"address":null,"new_img_nm":null,"p_pro_seq":0,"p_view_yn":null,"p_sido_cd":"","p_gugun_cd":"","p_store_nm":null,"p_theme_cd":null,"p_wireless_yn":null,"p_smoking_yn":null,"p_book_yn":null,"p_music_yn":null,"p_terrace_yn":null,"p_table_yn":null,"p_takeout_yn":null,"p_parking_yn":null,"p_dollar_assent":null,"p_card_recharge":null,"p_subway_yn":null,"stb_store_file_renew":null,"stb_store_theme_renew":null,"stb_store_time_renew":null,"stb_store_lsm":null,"s_code":"1047","s_name":"한국프레스센터","tel":"02-722-3263","fax":"02-722-3264","sido_code":"01","sido_name":"서울","gugun_code":"0119","gugun_name":"중구","addr":"서울특별시 중구 태평로1가 25 프레스센터","park_info":null,"new_state":null,"theme_state":"T08@T05@T09@P80@T04@T20@T16@T07@T17","new_bool":0,"search_text":"","ins_lat":"","ins_lng":"","in_distance":0,"out_distance":".09","all_search_cnt":-1,"addr_sear'
데이터는 JSON으로 수신되며, 형태는 다음과 같다.
{
"list": [
{
// ... 중략 ...
"s_code": "1311",
"s_name": "방화DT",
"tel": "02-2664-3480",
"fax": "02-2664-3481",
"sido_code": "01",
"sido_name": "서울",
"gugun_code": "0103",
"gugun_name": "강서구",
"addr": "서울특별시 강서구 방화동 293-4",
"park_info": null,
"new_state": null,
"theme_state": "T17@T16@T09@T20@T01@T05@T08@T04",
// ... 중략 ...
"lat": "37.574339",
"lot": "126.816415",
"t22": 0
},
{
// ... 중략 ...
"s_code": "1267",
"s_name": "마곡나루역",
"tel": "02-3662-3504",
"fax": "02-3662-3505",
"sido_code": "01",
"sido_name": "서울",
"gugun_code": "0103",
"gugun_name": "강서구",
"addr": "서울특별시 강서구 마곡동 759-3 보타닉파크타워Ⅰ105,203,204호",
"park_info": null,
"new_state": null,
"theme_state": "T08@T05@T04@T17@T16@P80@T20",
// ... 중략 ...
"lat": "37.56813",
"lot": "126.82614",
"t22": 0
},
]
}
JSON to DataFrame¶
json_normalize() 를 사용하여 JSON 데이터를 DataFrame로 전환
In [3]:
jo = json.loads(r.text)
df = json_normalize(jo, 'list')
In [4]:
df.head(5)
Out[4]:
In [19]:
# 행(row)수, 서울 450개 매장
len(df)
Out[19]:
In [20]:
# 컬럼수 111개
df.columns
Out[20]:
In [21]:
# 주요한 컬럼 몇 가지 선택
df = df[['s_name', 'lat', 'lot', 'doro_address', 'tel']]
df.head(10)
Out[21]:
In [15]:
df.dtypes
Out[15]:
In [13]:
df['lat'] = df['lat'].astype(float)
df['lot'] = df['lot'].astype(float)
In [ ]:
df.dtypes
In [23]:
df[df['s_name'] == '광화문']
Out[23]:
In [9]:
import folium
In [10]:
map_osm = folium.Map(location=(37.5712293, 126.9762872))
map_osm
Out[10]:
In [11]:
map_osm = folium.Map(location=(37.5712293, 126.9762872), zoom_start=17)
map_osm
Out[11]:
In [12]:
map_osm = folium.Map(location=(37.5712293, 126.9762872), zoom_start=17, tiles='Stamen Toner')
map_osm
Out[12]:
In [13]:
location=(37.5712293, 126.9762872)
map_osm = folium.Map(location=location, zoom_start=17)
folium.Marker(location, popup='광화문점').add_to(map_osm)
map_osm
Out[13]:
In [14]:
location=(37.5712293, 126.9762872)
map_osm = folium.Map(location=location, zoom_start=17)
folium.Marker(location, popup='광화문점').add_to(map_osm)
map_osm
Out[14]:
서울 스타벅스 전 지점¶
In [16]:
map_osm = folium.Map(location=location, zoom_start=11)
for ix, row in df_sum.iterrows():
location = (row['lat'], row['lot'])
folium.Marker(location, popup=row['s_name']).add_to(map_osm)
map_osm
In [ ]:
댓글 없음:
댓글 쓰기