memex-in's picture
Update app.py
42dce2f verified
raw
history blame
1.68 kB
import os
import streamlit as st
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image
# Function to capture a screenshot of a webpage
def capture_screenshot(url, output_path='screenshot.png'):
chrome_options = Options()
chrome_options.add_argument('--headless') # Run Chrome in headless mode
chrome_options.add_argument('--disable-gpu') # Disable GPU acceleration
chrome_options.add_argument('--no-sandbox') # Disable sandbox for better compatibility
chrome_options.add_argument('--window-size=1920,1080') # Set window size for consistent screenshots
driver = webdriver.Chrome(options=chrome_options)
try:
driver.get(url) # Open the URL
driver.save_screenshot(output_path) # Save screenshot to file
finally:
driver.quit() # Ensure the driver is closed even if an error occurs
# Streamlit app layout
st.title("Web Screenshot Viewer")
# Input field for URL
url = st.text_input("Enter a website URL:", "https://example.com")
# Button to trigger the screenshot process
if st.button("Generate Screenshot"):
try:
screenshot_file = "screenshot.png"
st.write("Capturing website screenshot...")
capture_screenshot(url.strip('"')) # Strip any extra quotes from the URL
# Display the screenshot in the Streamlit app
st.image(screenshot_file, caption='Screenshot of the webpage', use_column_width=True)
except Exception as e:
st.error(f"An error occurred: {e}")
finally:
# Clean up the screenshot file if it exists
if os.path.exists(screenshot_file):
os.remove(screenshot_file)