-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
169 lines (148 loc) · 6.15 KB
/
scraper.py
File metadata and controls
169 lines (148 loc) · 6.15 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
scraper.py — Pure DrissionPage Scraper
"""
from bs4 import BeautifulSoup, Comment
from DrissionPage import ChromiumPage, ChromiumOptions
import tempfile
import shutil
import random
import time
import os
import traceback
import json
def get_website_content(url: str, headless: bool = False, extraction_mode: str = "html") -> tuple[str | None, str]:
print(f"\n🕵️ Scraping (Pure DrissionPage): {url}")
is_server = bool(os.environ.get("RENDER") or os.environ.get("CHROMIUM_PATH"))
temp_user_data = tempfile.mkdtemp()
co = ChromiumOptions()
co.headless(headless or is_server)
co.set_argument('--headless=new')
if is_server:
co.set_argument('--no-sandbox')
co.set_argument('--disable-dev-shm-usage')
co.set_argument('--disable-gpu')
co.set_argument('--disable-software-rasterizer')
co.set_user_agent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
co.set_argument(f'--user-data-dir={temp_user_data}')
co.set_argument('--window-size=1280,720')
if is_server:
co.set_argument('--disable-extensions')
co.set_argument('--js-flags=--max-old-space-size=256')
co.set_argument('--single-process')
# Fix DrissionPage WebSocket issue in Docker:
co.set_argument('--remote-debugging-address=0.0.0.0')
debug_port = random.randint(9200, 9300)
co.set_argument(f'--remote-debugging-port={debug_port}')
co.set_local_port(debug_port)
else:
co.auto_port()
chromium_path = os.environ.get("CHROMIUM_PATH")
if chromium_path:
co.set_browser_path(chromium_path)
page = None
api_responses = []
total_api_bytes = 0
MAX_API_DATA_BYTES = 50_000
try:
# Retry logic for Docker cold starts
max_retries = 3 if is_server else 1
for attempt in range(max_retries):
try:
page = ChromiumPage(addr_or_opts=co)
break
except Exception as e:
print(f"⚠️ Browser attempt {attempt+1} failed: {e}")
if attempt == max_retries - 1:
print("❌ Browser connection failed completely.")
return None, ""
time.sleep(2)
page.run_js("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
# Only use listener locally to avoid Docker instability
use_listener = not is_server
if extraction_mode == "network":
print("[Scraper] Mode: Network Intercept Fast Path")
try:
page.listen.start(targets='api|graphql|json|v1|v2|query|search|feed')
except:
pass
page.set.timeouts(page_load=30, script=10)
print(f"🌐 Loading {url}...")
try:
page.get(url)
page.wait.doc_loaded() # Wait for document.readyState
except Exception as e:
print(f"⚠️ Page load warning: {e}")
if extraction_mode == "network":
print("⏳ Waiting for API responses (up to 8s)...")
try:
packet = page.listen.wait(timeout=8)
if packet and packet.response and packet.response.body:
try:
data = json.loads(packet.response.body)
api_responses.append({"url": packet.url[:200], "data": data})
total_api_bytes += len(packet.response.body)
except:
pass
except Exception as e:
print(f"⚠️ Listen wait timeout or err: {e}")
try:
page.listen.stop()
except:
pass
else:
print("⏳ Strict DOM Wait: Waiting for dynamic elements...")
try:
# Wait for article, main, or dynamically injected divs to appear
page.ele('css:article, main, div[id*="root"], div[id*="app"]', timeout=10)
except:
time.sleep(3) # Fallback for dynamic content mapping
# Scroll
for _ in range(4):
page.scroll.down(500)
time.sleep(0.5)
if use_listener:
# Fallback intercept logic from before for regular mode
try:
for packet in page.listen.steps(timeout=2):
if not packet.response: continue
body = packet.response.body
if not body or len(body) < 20: continue
try:
data = json.loads(body)
api_responses.append({"url": packet.url[:200], "data": data})
total_api_bytes += len(body)
if total_api_bytes > MAX_API_DATA_BYTES: break
except:
pass
except:
pass
try:
page.listen.stop()
except:
pass
raw_html = page.html
soup = BeautifulSoup(raw_html, "html.parser")
for tag in soup(["script", "style", "svg", "iframe", "noscript"]):
tag.decompose()
for comment in soup.find_all(string=lambda text: isinstance(text, Comment)):
comment.extract()
clean = " ".join(str(soup.body or soup).split())
api_data_str = ""
if api_responses:
api_data_str = json.dumps(api_responses, ensure_ascii=False)[:MAX_API_DATA_BYTES]
print(f"✅ Captured {len(clean):,} chars HTML + {len(api_data_str):,} chars API")
return clean[:300000], api_data_str
except Exception as e:
print(f"❌ Scraper error: {e}")
traceback.print_exc()
return None, ""
finally:
if page:
try:
page.quit()
except:
pass
try:
shutil.rmtree(temp_user_data, ignore_errors=True)
except:
pass