-driver = webdriver.Chrome(executable='../driver/chromedriver') # 드라이버 위치
-driver.quit() # 한 번에 모든 드라이버 창 종료
-driver.close() # 창 한 개 종료
-driver.find_element(By.CSS_SELECTOR, '#~~~') # 1개
-driver.find_elements(By.CSS_SELECTOR, '') # 여러 개
-keyword.clear() : 입력된 키워드 지우기
-복잡한 동작 실행 시, ActionChains 사용 (ex. before::)
-driver.switch_to_window(driver.window_handles[-1]) : 창 변경
import time
from selenium import webdriver
from tqdm import tqdm_notebook
url = "https://www.opinet.co.kr/searRgSelect.do"
driver = webdriver.Chrome()
driver.get(url)
driver.get(url)
# 지역 : 시/도
sido_list_raw = driver.find_element(By.ID, "SIDO_NM0")
sido_list = sido_list_raw.find_elements(By.TAG_NAME, "option")
sido_names = [option.get_attribute("value") for option in sido_list]
sido_names = sido_names[1:]
sido_list_raw.send_keys(sido_names[0])
# 구
gu_list_raw = driver.find_element(By.ID, "SIGUNGU_NM0")
gu_list = gu_list_raw.find_elements(By.TAG_NAME, "option")
gu_names = [option.get_attribute("value") for option in gu_list]
gu_names = gu_names[1:]
for gu in tqdm_notebook(gu_names):
element = driver.find_element(By.ID, "SIGUNGU_NM0")
element.send_keys(gu)
time.sleep(3)
element_get_excel = driver.find_element(By.ID, "glopopd_excel").click()
time.sleep(3)
import pandas as pd
from glob import glob
# 파일 목록 한 번에 가져오기
stations_files = glob("../data/지역_*.xls")
tmp_raw = []
for file_name in stations_files:
tmp = pd.read_excel(file_name, header=2)
tmp_raw.append(tmp)
station_raw = pd.concat(tmp_raw)
stations = pd.DataFrame({
"상호" : station_raw["상호"],
"주소" : station_raw["주소"],
"가격" : station_raw["휘발유"],
"셀프" : station_raw["셀프여부"],
"상표" : station_raw["상표"],
})
stations["구"] = [eachAddress.split()[1] for eachAddress in stations["주소"]]
stations
stations["가격"] = stations["가격"].astype(float)
stations.reset_index(inplace=True)
del stations["index"]
import matplotlib.pyplot as plt
import seaborn as sns
import platform
from matplotlib import font_manager, rc
%matplotlib inline
path = "C:/Windows/Fonts/malgun.ttf"
if platform.system() == "Darwin":
rc("font", family="Arial Unicode MS")
elif platform.system() == "Windows":
font_name = font_manager.FontProperties(fname=path).get_name()
rc("font", family=font_name)
else :
print("Unknown system. sorry")
# boxplot(pandas) - 셀프 유무
stations.boxplot(column="가격", by="셀프", figsize=(12,8));
# boxplot(seaborn) - 셀프 유무
plt.figure(figsize=(12,8))
sns.boxplot(x="셀프", y="가격", data=stations, palette="Set2")
plt.grid(True)
plt.show()
# boxplot(seaborn) - 상표별
plt.figure(figsize=(12,8))
sns.boxplot(x="상표", y="가격", hue="셀프", data=stations, palette="Set2")
plt.grid(True)
plt.show()
# 지도시각화
import json
import folium
import warnings
import numpy as np
warnings.simplefilter(action="ignore", category=FutureWarning)
gu_data = pd.pivot_table(data=stations, index="구", values="가격", aggfunc=np.mean)
geo_path = "../data/02. skorea_municipalities_geo_simple.json"
geo_str = json.load(open(geo_path, encoding="utf-8"))
my_map = folium.Map(location=[37.5502, 126.982], zoom_start=10.5, tiles="Stamen Toner")
my_map.choropleth(
geo_data=geo_str,
data=gu_data,
columns=[gu_data.index, "가격"],
key_on="feature.id",
fill_color="PuRd"
)
my_map