-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
77 lines (63 loc) · 2.66 KB
/
agent.py
File metadata and controls
77 lines (63 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import time
import re
import os
import tempfile
from playwright.sync_api import sync_playwright
def clean_url(url: str) -> str:
url = url.strip()
url = re.sub(r'^ht{2,}ps', 'https', url)
if not url.startswith(('http://', 'https://')):
print(f"⚠️ Scout: Assuming HTTPS for {url}")
return f"https://{url}"
return url
def scrape_website(url: str):
target_url = clean_url(url)
print(f"🕵️ Scout Agent: Heading to {target_url}...")
with sync_playwright() as p:
browser = p.chromium.launch(
headless=True,
args=["--disable-blink-features=AutomationControlled", "--no-sandbox"]
)
context = browser.new_context(
viewport={"width": 1400, "height": 1000},
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
page = context.new_page()
try:
page.goto(target_url, wait_until="domcontentloaded", timeout=60000)
# Scroll Logic
for i in range(5):
page.mouse.wheel(0, 500)
time.sleep(0.5)
page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(1)
page.evaluate("window.scrollTo(0, 0)")
time.sleep(1)
# --- PERMISSION FIX ---
# We construct a path inside the system's temporary directory
temp_dir = tempfile.gettempdir()
screenshot_path = os.path.join(temp_dir, "screenshot_input.jpg")
page.screenshot(
path=screenshot_path,
full_page=True,
quality=80,
type='jpeg'
)
print(f"📸 Scout Agent: Evidence secured at {screenshot_path}")
text_content = page.inner_text("body")
return {
"screenshot_path": screenshot_path,
"text_content": text_content,
"status": "success"
}
except Exception as e:
error_msg = str(e)
print(f"❌ Scout Agent Error: {error_msg}")
if "ERR_ABORTED" in error_msg or "ERR_NAME_NOT_RESOLVED" in error_msg:
return {"status": "error", "error": f"Could not reach {target_url}.", "screenshot_path": None}
return {"status": "error", "error": f"Browser Error: {error_msg[:100]}...", "screenshot_path": None}
finally:
browser.close()
# Test block
if __name__ == "__main__":
scrape_website("https://example.com")