Files
Agente-Marketing/casa-hunter/test_human.py
T

101 lines
3.7 KiB
Python

import re, json, time, random
from playwright.sync_api import sync_playwright
def human_delay(min_s=1.5, max_s=4.0):
time.sleep(random.uniform(min_s, max_s))
def slow_scroll(page, steps=5):
for i in range(steps):
page.mouse.wheel(0, random.randint(300, 600))
time.sleep(random.uniform(0.4, 0.9))
def test_zillow_human():
print("=== Zillow - comportamiento humano ===")
with sync_playwright() as p:
browser = p.chromium.launch(
headless=False, # Ventana visible - menos detectable
args=[
"--disable-blink-features=AutomationControlled",
"--no-sandbox",
"--start-maximized",
]
)
ctx = browser.new_context(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
locale="en-US",
timezone_id="America/New_York",
viewport={"width": 1366, "height": 768},
)
ctx.add_init_script("""
Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
window.chrome = {runtime: {}};
Object.defineProperty(navigator, 'plugins', {get: () => [1,2,3,4,5]});
""")
page = ctx.new_page()
# Primero ir a Google como un humano
print("Abriendo Google...")
page.goto("https://www.google.com", wait_until="load", timeout=30000)
human_delay(1, 2)
# Buscar Zillow en Google
page.fill("textarea[name=q], input[name=q]", "zillow vero beach florida homes for sale under 230000")
human_delay(0.5, 1)
page.keyboard.press("Enter")
page.wait_for_load_state("load", timeout=20000)
human_delay(1, 2)
# Ir directo a Zillow
print("Abriendo Zillow...")
page.goto("https://www.zillow.com/homes/for_sale/vero-beach-fl/", wait_until="load", timeout=45000)
human_delay(2, 4)
print("Title:", page.title()[:80])
slow_scroll(page, 4)
human_delay(1, 2)
content = page.content()
cards = page.query_selector_all("[data-test='property-card']")
print(f"Cards: {len(cards)}")
for card in cards[:5]:
print(" ", card.inner_text()[:120].replace('\n', ' | '))
if not cards:
prices = re.findall(r'"unformattedPrice":\s*(\d+)', content)
print("Prices en HTML:", prices[:5])
browser.close()
def test_realtor_human():
print("\n=== Realtor.com - comportamiento humano ===")
with sync_playwright() as p:
browser = p.chromium.launch(
headless=False,
args=["--disable-blink-features=AutomationControlled", "--start-maximized"]
)
ctx = browser.new_context(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
locale="en-US",
timezone_id="America/New_York",
)
ctx.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined});")
page = ctx.new_page()
page.goto("https://www.realtor.com", wait_until="load", timeout=30000)
human_delay(1.5, 3)
page.goto("https://www.realtor.com/realestateandhomes-search/Vero-Beach_FL/price-na-230000", wait_until="load", timeout=45000)
human_delay(2, 4)
slow_scroll(page, 5)
print("Title:", page.title()[:80])
cards = page.query_selector_all("[data-testid='property-card-content']")
print(f"Cards: {len(cards)}")
for card in cards[:3]:
print(" ", card.inner_text()[:120].replace('\n', ' | '))
browser.close()
test_zillow_human()
test_realtor_human()