| import streamlit as st |
| import requests |
| from io import BytesIO |
| import os |
|
|
| |
| st.title("Web Content Downloader") |
|
|
| |
| url_to_download = st.text_input("Enter the URL to download:") |
|
|
| |
| if url_to_download: |
| response = requests.get(url_to_download) |
| if response.status_code == 200: |
| st.success("Content successfully fetched from the URL.") |
| else: |
| st.error(f"Failed to fetch content from the URL. Status code: {response.status_code}") |
|
|
| |
| if url_to_download and response.status_code == 200: |
| |
| file_name = os.path.basename(url_to_download) |
|
|
| |
| st.markdown( |
| f"**[Download Content]({url_to_download})** - Right-click and choose 'Save link as...' to download" |
| ) |
|
|
| |
| file_bytes = BytesIO(response.content) |
| st.session_state["downloaded_file"] = {"file_bytes": file_bytes, "file_name": file_name} |
|
|
| |
| if st.button("Download Content"): |
| if "downloaded_file" in st.session_state: |
| downloaded_file = st.session_state["downloaded_file"] |
| st.download_button( |
| label="Click to download", |
| data=downloaded_file["file_bytes"].read(), |
| file_name=downloaded_file["file_name"], |
| key="download_button", |
| ) |
| else: |
| st.warning("No content available for download. Please fetch content from a URL.") |
|
|