75 lines
3.0 KiB
Python
75 lines
3.0 KiB
Python
from pywinauto import Application, findwindows, timings
|
|
from pywinauto.timings import wait_until
|
|
import logging
|
|
import time
|
|
import json
|
|
|
|
# 로깅 설정
|
|
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s")
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class ShoppingLensScraper:
|
|
def __init__(self, whale_window, logger=None):
|
|
self.whale_window = whale_window
|
|
"""네이버 웨일 실행 파일 경로 및 로거 설정"""
|
|
self.logger = logger or logging.getLogger(__name__)
|
|
|
|
def search(self, image_url):
|
|
"""이미지 URL을 쇼핑렌즈로 검색하고 결과를 반환"""
|
|
try:
|
|
# # 웨일 브라우저 실행
|
|
# app = Application(backend="uia").start(self.whale_exe_path)
|
|
|
|
# # '새 탭 - Whale' 창 대기 및 PID 가져오기
|
|
# timings.wait_until(10, 0.5, lambda: any(
|
|
# window.name == "새 탭 - Whale" for window in findwindows.find_elements()
|
|
# ))
|
|
# whale_pid = next(
|
|
# (window.process_id for window in findwindows.find_elements() if window.name == "새 탭 - Whale"), None
|
|
# )
|
|
|
|
# if not whale_pid:
|
|
# raise RuntimeError("'새 탭 - Whale' 창을 찾을 수 없습니다.")
|
|
|
|
# # 웨일 창 연결 및 제어
|
|
# app = Application(backend="uia").connect(process=whale_pid)
|
|
# whale_window = app.top_window()
|
|
# whale_window.set_focus()
|
|
|
|
# URL 이동
|
|
address_bar = self.whale_window.child_window(title="주소창 및 검색창", control_type="Edit")
|
|
address_bar.click_input()
|
|
for chunk in [image_url[i:i + 10] for i in range(0, len(image_url), 10)]:
|
|
address_bar.type_keys(chunk, with_spaces=True)
|
|
address_bar.type_keys("{ENTER}")
|
|
time.sleep(5) # 페이지 로딩 대기
|
|
|
|
# 이미지 우클릭 및 쇼핑 렌즈 검색
|
|
image = self.whale_window.child_window(control_type="Image")
|
|
image.right_click_input()
|
|
|
|
menu_item = self.whale_window.child_window(title="쇼핑렌즈로 검색하기", control_type="MenuItem")
|
|
menu_item.click_input()
|
|
|
|
# 결과 대기
|
|
wait_until(10, 0.5, lambda: self.whale_window.child_window(control_type="List").exists())
|
|
time.sleep(5) # 추가 대기
|
|
|
|
# 검색 결과 파싱
|
|
links = self.whale_window.descendants(control_type="Hyperlink")
|
|
products = [
|
|
{
|
|
"name": link.window_text(),
|
|
"price": "알 수 없음", # 필요 시 수정
|
|
"shipping": "알 수 없음" # 필요 시 수정
|
|
}
|
|
for link in links
|
|
]
|
|
|
|
self.logger.info(f"쇼핑 렌즈 결과: {json.dumps(products, ensure_ascii=False, indent=2)}")
|
|
return products
|
|
|
|
except Exception as e:
|
|
self.logger.error(f"쇼핑 렌즈 검색 실패: {e}")
|
|
return []
|