prince1604 commited on
Commit ·
dc99297
1
Parent(s): 1c616c4
Optimize memory usage to prevent crashes
Browse files- Dockerfile +1 -1
- src/crawler.py +12 -17
Dockerfile
CHANGED
|
@@ -31,4 +31,4 @@ EXPOSE 7860
|
|
| 31 |
|
| 32 |
# Command to run the application using Gunicorn
|
| 33 |
# Bind to 0.0.0.0:7860 as required by HF Spaces
|
| 34 |
-
CMD ["gunicorn", "-b", "0.0.0.0:7860", "--timeout", "120", "--workers", "2", "api:app"]
|
|
|
|
| 31 |
|
| 32 |
# Command to run the application using Gunicorn
|
| 33 |
# Bind to 0.0.0.0:7860 as required by HF Spaces
|
| 34 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "--timeout", "120", "--workers", "1", "--threads", "2", "api:app"]
|
src/crawler.py
CHANGED
|
@@ -149,6 +149,7 @@ class Crawler:
|
|
| 149 |
)
|
| 150 |
|
| 151 |
page = context.new_page()
|
|
|
|
| 152 |
try:
|
| 153 |
logger.info(f"Playwright navigating to {url}...")
|
| 154 |
page.goto(url, timeout=60000, wait_until="domcontentloaded")
|
|
@@ -160,30 +161,24 @@ class Crawler:
|
|
| 160 |
title = page.title()
|
| 161 |
logger.info(f"Page Title: {title}")
|
| 162 |
|
| 163 |
-
# CAPTCHA Detection via Selectors
|
| 164 |
-
# Amazon often uses #captchacharacters
|
| 165 |
-
if page.locator("input#captchacharacters").count() > 0 or "Robot Check" in title:
|
| 166 |
-
logger.error("Playwright hit visible CAPTCHA. Manual intervention required.")
|
| 167 |
-
self.blocked_reason = "Hard Catcha/Robot Check"
|
| 168 |
-
|
| 169 |
-
# Save screenshot for proof
|
| 170 |
-
page.screenshot(path="amazon.png")
|
| 171 |
-
logger.info("Saved screenshot to amazon.png")
|
| 172 |
-
|
| 173 |
-
# Save HTML
|
| 174 |
-
with open("debug_playwright.html", "w", encoding="utf-8") as f:
|
| 175 |
-
f.write(page.content())
|
| 176 |
-
|
| 177 |
-
return None # Return None to signify blocked/useless content
|
| 178 |
-
|
| 179 |
content = page.content()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
return content
|
| 181 |
|
| 182 |
except Exception as nav:
|
| 183 |
logger.error(f"Playwright navigation error: {nav}")
|
| 184 |
return None
|
| 185 |
finally:
|
| 186 |
-
browser
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
logger.error(f"Playwright critical error: {e}")
|
|
|
|
| 149 |
)
|
| 150 |
|
| 151 |
page = context.new_page()
|
| 152 |
+
try:
|
| 153 |
try:
|
| 154 |
logger.info(f"Playwright navigating to {url}...")
|
| 155 |
page.goto(url, timeout=60000, wait_until="domcontentloaded")
|
|
|
|
| 161 |
title = page.title()
|
| 162 |
logger.info(f"Page Title: {title}")
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
content = page.content()
|
| 165 |
+
|
| 166 |
+
# Explicit cleanup
|
| 167 |
+
page.close()
|
| 168 |
+
context.close()
|
| 169 |
+
browser.close()
|
| 170 |
+
|
| 171 |
return content
|
| 172 |
|
| 173 |
except Exception as nav:
|
| 174 |
logger.error(f"Playwright navigation error: {nav}")
|
| 175 |
return None
|
| 176 |
finally:
|
| 177 |
+
# Ensure browser is closed even if errors occur
|
| 178 |
+
try:
|
| 179 |
+
browser.close()
|
| 180 |
+
except:
|
| 181 |
+
pass
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
logger.error(f"Playwright critical error: {e}")
|