|
|
|
|
|
import streamlit as st |
|
from googlesearch import search |
|
import requests |
|
from bs4 import BeautifulSoup |
|
|
|
def get_first_link(query): |
|
try: |
|
for result in search(query, num=1, stop=1): |
|
return result |
|
except Exception as e: |
|
st.error(f"Error fetching search results: {e}") |
|
return None |
|
|
|
def download_webpage_content(url): |
|
try: |
|
response = requests.get(url) |
|
response.raise_for_status() |
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
return soup.prettify() |
|
except Exception as e: |
|
st.error(f"Error fetching webpage content: {e}") |
|
return None |
|
|
|
st.title("Webpage Content Downloader") |
|
|
|
query = st.text_input("Enter your search query:") |
|
|
|
if st.button("Fetch First Link and Download Content"): |
|
if query: |
|
with st.spinner("Fetching the first link..."): |
|
first_link = get_first_link(query) |
|
if first_link: |
|
st.success(f"First Link Found: {first_link}") |
|
with st.spinner("Downloading webpage content..."): |
|
webpage_content = download_webpage_content(first_link) |
|
if webpage_content: |
|
st.success("Content Downloaded!") |
|
st.download_button( |
|
label="Download Webpage Content", |
|
data=webpage_content, |
|
file_name="webpage_content.html", |
|
mime="text/html" |
|
) |
|
else: |
|
st.error("No links found for the query.") |
|
else: |
|
st.error("Please enter a query.") |
|
|
|
|