Spaces:
Paused
Paused
Create stealth_browser.py
Browse files- stealth_browser.py +105 -0
stealth_browser.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from playwright.sync_api import sync_playwright
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# Human-like delay
|
| 7 |
+
# -----------------------------
|
| 8 |
+
def human_delay(min_ms=200, max_ms=800):
|
| 9 |
+
time.sleep(random.uniform(min_ms / 1000, max_ms / 1000))
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# -----------------------------
|
| 13 |
+
# Stealth browser launcher
|
| 14 |
+
# -----------------------------
|
| 15 |
+
def launch_stealth_browser(headless=True):
|
| 16 |
+
p = sync_playwright().start()
|
| 17 |
+
|
| 18 |
+
browser = p.chromium.launch(
|
| 19 |
+
headless=headless,
|
| 20 |
+
args=[
|
| 21 |
+
"--disable-blink-features=AutomationControlled",
|
| 22 |
+
"--disable-infobars",
|
| 23 |
+
"--disable-dev-shm-usage",
|
| 24 |
+
"--no-sandbox",
|
| 25 |
+
"--disable-gpu",
|
| 26 |
+
"--disable-extensions",
|
| 27 |
+
]
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
context = browser.new_context(
|
| 31 |
+
user_agent=random_user_agent(),
|
| 32 |
+
viewport=random_viewport(),
|
| 33 |
+
locale="en-US",
|
| 34 |
+
timezone_id="America/New_York",
|
| 35 |
+
permissions=["geolocation"],
|
| 36 |
+
java_script_enabled=True,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Mask webdriver + automation traces
|
| 40 |
+
context.add_init_script("""
|
| 41 |
+
Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
|
| 42 |
+
Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
|
| 43 |
+
Object.defineProperty(navigator, 'plugins', {
|
| 44 |
+
get: () => [1, 2, 3, 4, 5]
|
| 45 |
+
});
|
| 46 |
+
""")
|
| 47 |
+
|
| 48 |
+
page = context.new_page()
|
| 49 |
+
return p, browser, context, page
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# -----------------------------
|
| 53 |
+
# Random viewport
|
| 54 |
+
# -----------------------------
|
| 55 |
+
def random_viewport():
|
| 56 |
+
return {
|
| 57 |
+
"width": random.choice([1366, 1440, 1536, 1600, 1920]),
|
| 58 |
+
"height": random.choice([768, 900, 1024, 1080]),
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# -----------------------------
|
| 63 |
+
# High-quality user agents
|
| 64 |
+
# -----------------------------
|
| 65 |
+
def random_user_agent():
|
| 66 |
+
return random.choice([
|
| 67 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
| 68 |
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15",
|
| 69 |
+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
|
| 70 |
+
])
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# -----------------------------
|
| 74 |
+
# Cloudflare-aware navigation
|
| 75 |
+
# -----------------------------
|
| 76 |
+
def stealth_goto(page, url):
|
| 77 |
+
page.goto(url, wait_until="domcontentloaded", timeout=45000)
|
| 78 |
+
|
| 79 |
+
human_delay(800, 1600)
|
| 80 |
+
|
| 81 |
+
# Simulate small mouse movements
|
| 82 |
+
for _ in range(random.randint(2, 5)):
|
| 83 |
+
page.mouse.move(
|
| 84 |
+
random.randint(100, 800),
|
| 85 |
+
random.randint(100, 600),
|
| 86 |
+
steps=random.randint(5, 20)
|
| 87 |
+
)
|
| 88 |
+
human_delay(100, 300)
|
| 89 |
+
|
| 90 |
+
# Scroll like human
|
| 91 |
+
page.mouse.wheel(0, random.randint(300, 900))
|
| 92 |
+
human_delay(500, 1200)
|
| 93 |
+
|
| 94 |
+
return page.content()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# -----------------------------
|
| 98 |
+
# Close browser safely
|
| 99 |
+
# -----------------------------
|
| 100 |
+
def close_browser(p, browser):
|
| 101 |
+
try:
|
| 102 |
+
browser.close()
|
| 103 |
+
p.stop()
|
| 104 |
+
except:
|
| 105 |
+
pass
|