Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- HTML
- 판다스 데이터정렬
- MachineLearning
- NLP
- 딥러닝
- 사이킷런
- 랭체인
- fastapi #python웹개발
- programmablesearchengine
- 파이썬웹개발
- chatGPT
- 머신러닝
- 파이토치기본
- fastapi
- fastapi #파이썬웹개발
- pytorch
- OpenAIAPI
- 비지도학습
- pandas
- sklearn
- konlpy
- 챗gpt
- deeplearning
- 판다스
- python 정렬
- langchain
- 파이토치
- Python
- 파이썬
- 자연어분석
Archives
- Today
- Total
Data Navigator
[공공데이터 수집] 전국 치킨집 자료 수집 - 전국 상권 정보 API 활용 본문
In [1]:
import requests
from bs4 import BeautifulSoup
import json
import os
import sys
import folium
import pymysql
import csv
In [8]:
# 소분류로 검색: storeListInUpjong
# 소분류 코드: O,Q,음식,Q05,닭/오리요리,Q05A08,후라이드/양념치킨
# 마지막 수집일: 2020.11.28
chickenShopsList = []
for repeat in range(1,2):
url = 'http://apis.data.go.kr/B553077/api/open/sdsc/storeListInUpjong?'
url += 'divId=indsSclsCd&key=Q05A08&numOfRows=1000&pageNo={0}'.format(str(repeat))
url += '&type=json&ServiceKey=46kyRJ49WNDCbot3XpPvourHjckhU7JeA1KnF66Zy'
url += 'I0ptMlZNGMacC%2Fo7Am%2BPv3Y5x5KjEChjhC1D0yOboJ3Sg%3D%3D'
# response = requests.get(url)
text = requests.get(url).json()
chickenShopsList.append(text)
In [7]:
chickenShopsList[0]['header']['stdrYm']
Out[7]:
'202009'
In [9]:
# DB connection
def DBconnection(stdrYm,shopId,shopname,shopIdentity, shop_address1,city_code, shop_address2, gu_code, shop_address3, dong_law_code,
shop_old_address, shop_road_address, shop_floor, shop_sNo, coordinateY, coordinateX):
host = 'localhost'
user = 'root'
password = 'password'
db = 'bigdata'
conn = pymysql.connect(host=host, user=user, password=password, db=db)
# DB connection end
with conn.cursor() as cursor:
sql = "INSERT INTO `chickenshops` (`stdrYm`,`shopId`,`shopname`,`shopIdentity`, `shop_address1`, `city_code`, `shop_address2`, `gu_code`, \
`shop_address3`, `dong_law_code`, `shop_old_address`, `shop_road_address`, `shop_floor`, `shop_sNo`, `coordinateY`, `coordinateX`) \
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(sql, (stdrYm,shopId,shopname,shopIdentity, shop_address1, city_code, shop_address2, gu_code, shop_address3, dong_law_code,
shop_old_address, shop_road_address, shop_floor, shop_sNo, coordinateY, coordinateX))
conn.commit()
In [10]:
#['header']['stdrYm] #자료 기준 년월
# chickenShopsList
dataforMap = []
dataforDB =[]
n = 0
for page in range(len(chickenShopsList)):
for row_num in (range(len(chickenShopsList[page]['body']['items']))):
n += 1
stdrYm = chickenShopsList[page]['header']['stdrYm']
shop = chickenShopsList[page]['body']['items'][row_num]
shopId = shop['bizesId'] #가게 ID
shopname = shop['bizesNm'] #가게명
shopIdentity = shop['ksicNm'] #가게 세부업종
shop_address1 = shop['ctprvnNm'] #시
city_code = shop['ctprvnCd'] # citycode
shop_address2 = shop['signguNm'] #구
gu_code = shop['signguCd'] # 구 Code
shop_address3 = shop['ldongNm'] #법정동
dong_law_code = shop['ldongCd'] # 법정동 코드
shop_old_address = shop['lnoAdr'] # 구 주소
shop_road_address = shop['rdnmAdr'] # 도로명 주소
shop_floor = shop['flrNo'] #층 정보
shop_sNo = shop['hoNo'] #호 수
coordinateY = str(shop['lon']) #경도
coordinateX = str(shop['lat']) # 위도
shopDatas = (n, stdrYm, shopname, shop_road_address,coordinateX, coordinateY)
print(shopDatas)
# DBconnection(stdrYm, shopId,shopname,shopIdentity, shop_address1, city_code, shop_address2, gu_code, shop_address3, dong_law_code,
# shop_old_address, shop_road_address, shop_floor, shop_sNo, coordinateY, coordinateX)
# makeCSV(stdrYm, shopId,shopname,shopIdentity, shop_address1, city_code, shop_address2, gu_code, shop_address3,
# dong_law_code, shop_old_address, shop_road_address, shop_floor, shop_sNo, coordinateY, coordinateX)
# dataforMap.append(shopDatas)
# dataforDB.append(shopDatas4DB)
(1, '202009', '장모님치킨가창점', '대구광역시 달성군 가창면 가창동로 6', '35.7826625402814', '128.644458591419')
(2, '202009', '정통숯불바베큐치킨상인점', '대구광역시 달서구 상원로 46', '35.8139657995118', '128.536144333086')
(3, '202009', '짱구피자치킨', '경기도 동두천시 못골로 30', '37.9012616518346', '127.06590346074')
(4, '202009', '쭈노치킨', '경기도 김포시 고촌읍 장차로13번길 18', '37.6037515030602', '126.769859060814')
(5, '202009', '챠밍치킨', '강원도 원주시 문막읍 원문로 1570', '37.3219032387451', '127.833839796514')
(6, '202009', '치킨더홈원당점', '경기도 고양시 덕양구 호국로 840', '37.6597588880157', '126.840144186117')
(7, '202009', '치킨뱅이', '강원도 원주시 금불3길 5', '37.3483579547022', '127.932025388709')
(8, '202009', '치킨월드', '전라북도 익산시 선화로13길 49', '35.9523651201959', '126.943685518105')
(9, '202009', '치킨월드율량점', '충청북도 청주시 청원구 공항로138번길 53', '36.6698161542425', '127.487585541514')
(10, '202009', '치킨특공대', '경기도 수원시 영통구 청명로 130', '37.2568558606058', '127.074449397134')
.
.
.
(993, '202009', '교촌치킨', '경기도 용인시 기흥구 구성로 19', '37.2951415769226', '127.111866295261')
(994, '202009', '멕시카나', '인천광역시 계양구 새벌로 90', '37.5248498897642', '126.716627028847')
(995, '202009', '멕시카나치킨', '경기도 군포시 오금로 25', '37.3525519279529', '126.931280466069')
(996, '202009', '멕시칸숯불치킨', '경상북도 김천시 시청6길 46', '36.1425104566136', '128.115495131303')
(997, '202009', '땡큐맘치킨전주평화점', '전라북도 전주시 완산구 평화7길 40', '35.7850937532966', '127.139148414325')
(998, '202009', '바니치킨', '경기도 안산시 단원구 풍전로 7', '37.3225768379959', '126.787691949343')
(999, '202009', '땅땅치킨', '부산광역시 사상구 운산로 36', '35.1674997465042', '128.98038334192')
(1000, '202009', '굽네치킨', '부산광역시 북구 만덕2로44번길 72', '35.2109072793164', '129.033919983444')
In [ ]:
In [ ]:
len(dataforMap)
dataforDB
In [17]:
fname = '전국치킨집목록'
with open(fname,'w', encoding='utf8') as f:
chicken = json.dumps(chickenShopsList,indent=4, sort_keys=True, ensure_ascii=False)
f.write(chicken)
In [16]:
# CSV로 저장
def makeCSV(stdrYm, shopId,shopname,shopIdentity, shop_address1, city_code, shop_address2, gu_code, shop_address3, dong_law_code,
shop_old_address, shop_road_address, shop_floor, shop_sNo, coordinateY, coordinateX):
headers = ['stdrYm','shopId','shopname','shopIdentity', 'shop_address1', 'city_code' 'shop_address2' 'gu_code', 'shop_address3', 'dong_law_code',
'shop_old_address', 'shop_road_address', 'shop_floor', 'shop_sNo', 'coordinateY', 'coordinateX']
rows = [{'stdrYm': stdrYm, 'shopId':shopId ,'shopname': shopname,'shopIdentity': shopIdentity, 'shop_address1': shop_address1, 'city_code' : city_code,
'shop_address2': shop_address2, 'gu_code' : gu_code, 'shop_address3': shop_address3, 'dong_law_code' : dong_law_code, 'shop_old_address': shop_old_address,
'shop_road_address': shop_road_address, 'shop_floor':shop_floor, 'shop_sNo': shop_sNo,
'coordinateY': coordinateY, 'coordinateX': coordinateX}]
with open('chicken_shops.csv', 'a', newline='') as f:
f_cav = csv.DictWriter(f, headers)
f_cav.writeheader()
f_cav.writerows(rows)
In [ ]:
# 지도에 표시하기
# map = folium.Map(location= (37.566345, 126.977893), zoom_start=12)
# for i in range(len(dataforMap)):
# folium.Marker((dataforMap[i][3], dataforMap[i][4]), popup=dataforMap[i][1], icon=folium.Icon(color='red', icon='flag')).add_to(map)
# map.save("chicken_map.html")
# map
In [ ]:
dataforDB[12][1]
In [ ]:
'DataCollectingProject' 카테고리의 다른 글
K wether에서 날씨 지수 정보 추출하기 (0) | 2021.01.16 |
---|---|
[카카오 지도] 다음 뉴스 기사를 분석하여 식신로드 맛집 정보 추출 후 지도에 표시하기 (0) | 2021.01.16 |