Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,35 +3,41 @@ from selenium import webdriver
|
|
| 3 |
from selenium.common.exceptions import WebDriverException
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
-
import time
|
| 7 |
|
| 8 |
def take_screenshot(url):
|
| 9 |
options = webdriver.ChromeOptions()
|
| 10 |
-
options.add_argument(
|
| 11 |
-
options.add_argument(
|
| 12 |
-
options.add_argument(
|
| 13 |
|
|
|
|
| 14 |
try:
|
| 15 |
wd = webdriver.Chrome(options=options)
|
| 16 |
-
wd.set_window_size(
|
| 17 |
wd.get(url)
|
| 18 |
-
|
| 19 |
-
time.sleep(2) # <-- Add delay before screenshot
|
| 20 |
screenshot = wd.get_screenshot_as_png()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
except WebDriverException as e:
|
| 22 |
-
|
|
|
|
| 23 |
finally:
|
| 24 |
if wd:
|
| 25 |
wd.quit()
|
| 26 |
|
| 27 |
-
return Image.open(BytesIO(screenshot))
|
| 28 |
-
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=take_screenshot,
|
| 31 |
-
inputs=gr.Textbox(label="Website URL", value="https://github.com
|
| 32 |
-
outputs=gr.Image(type="pil", label="Screenshot"
|
| 33 |
title="Website Screenshot",
|
| 34 |
-
description="
|
| 35 |
)
|
| 36 |
|
| 37 |
iface.launch()
|
|
|
|
| 3 |
from selenium.common.exceptions import WebDriverException
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
def take_screenshot(url):
|
| 9 |
options = webdriver.ChromeOptions()
|
| 10 |
+
options.add_argument("--headless")
|
| 11 |
+
options.add_argument("--no-sandbox")
|
| 12 |
+
options.add_argument("--disable-dev-shm-usage")
|
| 13 |
|
| 14 |
+
wd = None
|
| 15 |
try:
|
| 16 |
wd = webdriver.Chrome(options=options)
|
| 17 |
+
wd.set_window_size(1280, 720)
|
| 18 |
wd.get(url)
|
| 19 |
+
time.sleep(2)
|
|
|
|
| 20 |
screenshot = wd.get_screenshot_as_png()
|
| 21 |
+
|
| 22 |
+
if len(screenshot) < 1000:
|
| 23 |
+
print("⚠ Screenshot too small, likely error.")
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
return Image.open(BytesIO(screenshot))
|
| 27 |
+
|
| 28 |
except WebDriverException as e:
|
| 29 |
+
print("WebDriverException:", e)
|
| 30 |
+
return None
|
| 31 |
finally:
|
| 32 |
if wd:
|
| 33 |
wd.quit()
|
| 34 |
|
|
|
|
|
|
|
| 35 |
iface = gr.Interface(
|
| 36 |
fn=take_screenshot,
|
| 37 |
+
inputs=gr.Textbox(label="Website URL", value="https://github.com"),
|
| 38 |
+
outputs=gr.Image(type="pil", label="Screenshot"),
|
| 39 |
title="Website Screenshot",
|
| 40 |
+
description="Takes a screenshot of the website and displays it here."
|
| 41 |
)
|
| 42 |
|
| 43 |
iface.launch()
|