관리 메뉴

DeseoDeSeo

[Python] 네이버 지도 데이터 수집 본문

Python

[Python] 네이버 지도 데이터 수집

deseodeseo 2023. 8. 29. 12:39
from selenium import webdriver as wb
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys

 

1. 크롬 드라이버를 실행해서 네이버 지도 사이트로 이동
driver =wb.Chrome()
driver.get("https://map.naver.com/p/")

 

2. 검색창에 검색어를 입력
:
네이버 지도에서는 ID값이 화면창에 따라서 매번 달라짐. 그래서 class name 사용함.
  ( = 검색창의 구분자인 id값이 실행 할 때 마다 변한다. )
   => 그래서 다른 선택자를 활용해야한다.
search = driver.find_element(By.CSS_SELECTOR,".input_search")
search.send_keys("동명동회식")
search.send_keys(Keys.ENTER)

 

3. 컴퓨터에게 바라보고 있는 창을 원하는 iframe으로 변경
     ( = > frame을 변경할 때는 switch_to.frame("iframe의 id" )
driver.switch_to.frame("searchIframe")

 

화면에 출력되지만 클릭이 안 되면, iframe임. 
iframe을 검사해라.
( html위에 html이 있는 구조.)
1. req는 iframe src를 찾아가서 거기서 데이터 수집한다. 
2. selenium에서는 현재 컴퓨터가 바라보고있는 html창을 변경.

 

4. 미미원을 클릭해서 세부정보를 조회

< 여러개 가져오면 ' elements ' 이다. >

title = driver.find_elements(By.CSS_SELECTOR,".place_bluelink.YwYLL")
title[0].click()

 

5. 현재 화면을상세정보 iframe으로  이동
( ⊙
iframe에서 다른 iframe으로 바로 이동 불가능!  )
(반드시 원본 데이터로 이동 한 다음에 다른 iframe으로 이동해야함 )
driver.switch_to.default_content()

 

6. 상세정보 iframe으로 이동
driver.switch_to.frame('entryIframe')

 

7. 상세정보에서 가게이름 정보 수집
name = driver.find_element(By.CSS_SELECTOR,".Fc1rA")
name.text

# 황톳길(검색내용 2번째) 클릭 -> 가게 이름 수집
# 원본데이터 -> 검색프레임 -> 2번째 가게 클릭
# -> 원본데이터 -> 상세정보 프레임 -> 데이터수집
driver.switch_to.default_content()
driver.switch_to.frame("searchIframe")
time.sleep(1)
title = driver.find_elements(By.CSS_SELECTOR,".place_bluelink.YwYLL")
title[1].click()
time.sleep(3)
driver.switch_to.default_content()
driver.switch_to.frame('entryIframe')
time.sleep(1)
name = driver.find_element(By.CSS_SELECTOR,".Fc1rA")
name.text