File size: 1,678 Bytes
4c475a5
 
 
 
42dce2f
4c475a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42dce2f
4c475a5
 
 
 
42dce2f
 
4c475a5
 
 
5951131
4c475a5
42dce2f
 
4c475a5
 
 
 
 
 
5951131
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
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)