클로크브라우저 자동화 실습 가이드
클로크브라우저(CloakBrowser)를 활용해 탐지를 우회하는 스텔스(Stealth) 크롬 환경에서 브라우저 자동화를 구축하는 튜토리얼입니다. 구글 코랩(Colab)과 같은 비동기 루프 환경에서 발생하는 오류를 스레드 분리로 해결하고, 세션 상태 저장 및 브라우저 신호 검출 등 핵심 실습 과정을 다룹니다. 웹 스크래핑 및 자동화 실무자들이 계정 보호와 안정적인 작업 수행을 위해 참고할 만한 내용입니다.
에디터 추천 | 에이전트 AI | AI 에이전트 | 튜토리얼
이 튜토리얼에서는 스텔스(Stealth) 크롬 환경에서 Playwright 스타일의 API를 사용하는 파이썬 친화적인 브라우저 자동화 도구인 클로크브라우저(CloakBrowser)를 살펴봅니다. 먼저 클로크브라우저를 설정하고 필요한 브라우저 바이너리를 준비한 뒤, 별도의 워커 스레드에서 동기식 브라우저 워크플로를 실행하여 코랩(Colab) 환경에서 자주 발생하는 asyncio 루프 충돌 문제를 해결합니다.
이어서 실질적인 자동화 단계를 진행합니다. 여기에는 브라우저 실행, 맞춤형 브라우저 컨텍스트 생성, 브라우저에서 감지되는 신호 검사, 로컬 테스트 페이지 상호작용, 세션 상태 저장, localStorage 복원, 영구적인 브라우저 프로필 사용, 스크린샷 캡처, 그리고 파싱을 위한 렌더링된 페이지 콘텐츠 추출 등이 포함됩니다.
코드 복사 | 다른 브라우저 사용
import os import sys import json import time import shutil import base64 import subprocess import concurrent.futures from pathlib import Path from datetime import datetime from textwrap import dedent
def run_cmd(cmd, check=True, capture=False): print(f"\n$ {' '.join(cmd)}") result = subprocess.run( cmd, check=check, text=True, stdout=subprocess.PIPE if capture else None, stderr=subprocess.STDOUT if capture else None, ) if capture and result.stdout: print(result.stdout[:4000]) return result
print("CloakBrowser 및 헬퍼 패키지 설치 중...") run_cmd([ sys.executable, "-m", "pip", "install", "-q", "-U", "cloakbrowser", "playwright", "pandas", "beautifulsoup4" ])
print("\nColab을 위한 Chromium 런타임 종속성 설치 중...") try: run_cmd([sys.executable, "-m", "playwright", "install-deps", "chromium"], check=False) except Exception as e: print("종속성 설치 프로그램 경고:", repr(e))
from cloakbrowser import ( launch, launch_context, launch_persistent_context, ensure_binary, binary_info, ) import pandas as pd from bs4 import BeautifulSoup from IPython.display import display, Image
WORKDIR = Path("/content/cloakbrowser_advanced_tutorial") WORKDIR.mkdir(parents=True, exist_ok=True)
SCREENSHOT_PATH = WORKDIR / "cloakbrowser_result.png" STORAGE_STATE_PATH = WORKDIR / "storage_state.json" PROFILE_DIR = WORKDIR / "persistent_profile"
print("\nCloakBrowser 바이너리 준비 중...") try: ensure_binary() except Exception as e: print("바이너리 설정 경고:", repr(e))
print("\nCloakBrowser 바이너리 정보:") try: info = binary_info() print(json.dumps(info, indent=2, default=str)) except Exception as e: print("바이너리 정보를 읽을 수 없습니다:", repr(e))
먼저 코랩 환경에서 브라우저 자동화와 결과 분석을 원활하게 진행할 수 있도록 클로크브라우저, Playwright, pandas, BeautifulSoup을 설치합니다. 또한 Chromium 런타임 종속성을 설치하고, 클로크브라우저의 주요 실행 유틸리티를 불러온 다음 스크린샷, 저장소 상태, 영구 프로필을 위한 작업 경로를 설정합니다. 그런 다음 자동화를 실행하기 전에 클로크브라우저 바이너리를 준비하고 브라우저 엔진이 올바르게 설치되었는지 확인하기 위해 세부 정보를 출력합니다.
코드 복사 | 다른 브라우저 사용
def make_data_url(html: str) -> str: encoded = base64.b64encode(html.encode("utf-8")).decode("ascii") return f"data:text/html;base64,{encoded}"
def print_section(title): print("\n" + "=" * 80) print(title) print("=" * 80)
def safe_close(obj, label="object"): try: if obj: obj.close() except Exception as e: print(f"{label}을(를) 닫는 중 경고 발생: {e}")
def run_sync_browser_job_in_thread(fn, *args, **kwargs): """ Google Colab 및 Jupyter에는 이미 asyncio 이벤트 루프가 실행되고 있습니다. 클로크브라우저는 현재 다음과 같은 Playwright 스타일의 동기식 헬퍼를 제공합니다: - launch() - launch_context() - launch_persistent_context()
Playwright의 동기식 API는 이미 실행 중인 이벤트 루프 내에서는 작동할 수 없습니다.
따라서 전체 브라우저 자동화 작업을 별도의 스레드 내에서 실행합니다.
"""
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(fn, *args, **kwargs)
return future.result()
test_page_html = dedent("""