File size: 1,655 Bytes
0d9ed80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51

# app.py
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.")