Initialize the global browser instance with anti-bot measures
()
| 26 | from playwright_stealth import stealth_async |
| 27 | |
| 28 | async def initialize_browser(): |
| 29 | """Initialize the global browser instance with anti-bot measures""" |
| 30 | global global_browser, global_context, global_page |
| 31 | if global_browser is None: |
| 32 | playwright = await async_playwright().__aenter__() |
| 33 | |
| 34 | global_browser = await playwright.chromium.launch( |
| 35 | headless=False, # headless=False can reduce bot detection in some cases |
| 36 | args=[ |
| 37 | '--no-sandbox', |
| 38 | '--disable-dev-shm-usage', |
| 39 | '--disable-blink-features=AutomationControlled', |
| 40 | ] |
| 41 | ) |
| 42 | |
| 43 | global_context = await global_browser.new_context( |
| 44 | user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' |
| 45 | 'AppleWebKit/537.36 (KHTML, like Gecko) ' |
| 46 | 'Chrome/116.0.0.0 Safari/537.36', |
| 47 | viewport={'width': 1280, 'height': 720}, |
| 48 | locale='en-US', |
| 49 | timezone_id='America/New_York', |
| 50 | color_scheme='light', |
| 51 | ) |
| 52 | |
| 53 | global_page = await global_context.new_page() |
| 54 | |
| 55 | # Apply stealth measures |
| 56 | await stealth_async(global_page) |
| 57 | |
| 58 | global_page.set_default_timeout(10000) |
| 59 | |
| 60 | def clean_text(text): |
| 61 | """Clean and normalize text content""" |
no outgoing calls
no test coverage detected