|
2 | 2 | 네이버 지도 장소 스크래핑 로직 |
3 | 3 | """ |
4 | 4 | import logging |
5 | | -import asyncio |
6 | 5 | from urllib.parse import quote |
7 | 6 |
|
8 | 7 | from playwright.async_api import async_playwright |
|
14 | 13 | parse_rating, |
15 | 14 | extract_naver_place_id_from_url, |
16 | 15 | SCRAPE_TIMEOUT_MS, |
17 | | - PAGE_LOAD_WAIT_SEC, |
18 | 16 | ELEMENT_WAIT_TIMEOUT_MS, |
19 | 17 | MAX_IMAGE_COUNT, |
20 | 18 | ) |
@@ -107,9 +105,8 @@ async def search_and_scrape(self, query: str) -> NaverPlaceInfo: |
107 | 105 |
|
108 | 106 | logger.info("첫 번째 결과 클릭 완료, 상세 페이지 로드 대기...") |
109 | 107 |
|
110 | | - # [5/7] 상세 페이지 로드 대기 |
| 108 | + # [5/7] 상세 페이지 로드 대기 (entryIframe이 나타날 때까지) |
111 | 109 | logger.info("[5/7] 상세 페이지 로드 대기...") |
112 | | - await asyncio.sleep(PAGE_LOAD_WAIT_SEC) |
113 | 110 |
|
114 | 111 | # entryIframe 대기 |
115 | 112 | logger.info("entryIframe 대기...") |
@@ -149,8 +146,22 @@ async def search_and_scrape(self, query: str) -> NaverPlaceInfo: |
149 | 146 |
|
150 | 147 | logger.debug(f"entry_frame URL: {entry_frame.url}") |
151 | 148 |
|
152 | | - # 추가 대기: DOM이 완전히 로드될 때까지 |
153 | | - await asyncio.sleep(2) |
| 149 | + # DOM 완전 로드 대기 (주소 요소가 나타날 때까지) |
| 150 | + try: |
| 151 | + await entry_frame.wait_for_selector('span.LDgIH', timeout=ELEMENT_WAIT_TIMEOUT_MS) |
| 152 | + except Exception: |
| 153 | + pass # 주소가 없는 장소도 있으므로 무시 |
| 154 | + |
| 155 | + # "찾아가는 길" 더보기 버튼 클릭 (전체 텍스트 펼치기) |
| 156 | + try: |
| 157 | + directions_expand_button = entry_frame.locator('a.xHaT3[aria-expanded="false"]') |
| 158 | + if await directions_expand_button.count() > 0: |
| 159 | + await directions_expand_button.first.click() |
| 160 | + # 펼쳐진 상태 대기 |
| 161 | + await entry_frame.wait_for_selector('a.xHaT3[aria-expanded="true"]', timeout=2000) |
| 162 | + logger.debug("찾아가는 길 더보기 클릭 완료") |
| 163 | + except Exception as error: |
| 164 | + logger.debug(f"찾아가는 길 더보기 클릭 실패 (무시): {error}") |
154 | 165 |
|
155 | 166 | # iframe 내에서 JavaScript 실행 |
156 | 167 | info = await entry_frame.evaluate(f'''() => {{ |
@@ -204,8 +215,10 @@ async def search_and_scrape(self, query: str) -> NaverPlaceInfo: |
204 | 215 | const subwayElement = document.querySelector('div.nZapA'); |
205 | 216 | result.subway_info = subwayElement ? subwayElement.textContent.trim() : null; |
206 | 217 |
|
207 | | - // 찾아가는 길 |
208 | | - const directionsElement = document.querySelector('span.zPfVt') || |
| 218 | + // 찾아가는 길 (전체 텍스트 - 펼쳐진 상태 우선) |
| 219 | + const expandedDirections = document.querySelector('a.xHaT3[aria-expanded="true"] span.zPfVt'); |
| 220 | + const directionsElement = expandedDirections || |
| 221 | + document.querySelector('span.zPfVt') || |
209 | 222 | document.querySelector('.place_section_content .zPfVt'); |
210 | 223 | result.directions_text = directionsElement ? directionsElement.textContent.trim() : null; |
211 | 224 |
|
|
0 commit comments