Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# app.py
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from googlesearch import search
|
| 5 |
+
import requests
|
| 6 |
+
from bs4 import BeautifulSoup
|
| 7 |
+
|
| 8 |
+
def get_first_link(query):
|
| 9 |
+
try:
|
| 10 |
+
for result in search(query, num=1, stop=1):
|
| 11 |
+
return result
|
| 12 |
+
except Exception as e:
|
| 13 |
+
st.error(f"Error fetching search results: {e}")
|
| 14 |
+
return None
|
| 15 |
+
|
| 16 |
+
def download_webpage_content(url):
|
| 17 |
+
try:
|
| 18 |
+
response = requests.get(url)
|
| 19 |
+
response.raise_for_status()
|
| 20 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 21 |
+
return soup.prettify()
|
| 22 |
+
except Exception as e:
|
| 23 |
+
st.error(f"Error fetching webpage content: {e}")
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
st.title("Webpage Content Downloader")
|
| 27 |
+
|
| 28 |
+
query = st.text_input("Enter your search query:")
|
| 29 |
+
|
| 30 |
+
if st.button("Fetch First Link and Download Content"):
|
| 31 |
+
if query:
|
| 32 |
+
with st.spinner("Fetching the first link..."):
|
| 33 |
+
first_link = get_first_link(query)
|
| 34 |
+
if first_link:
|
| 35 |
+
st.success(f"First Link Found: {first_link}")
|
| 36 |
+
with st.spinner("Downloading webpage content..."):
|
| 37 |
+
webpage_content = download_webpage_content(first_link)
|
| 38 |
+
if webpage_content:
|
| 39 |
+
st.success("Content Downloaded!")
|
| 40 |
+
st.download_button(
|
| 41 |
+
label="Download Webpage Content",
|
| 42 |
+
data=webpage_content,
|
| 43 |
+
file_name="webpage_content.html",
|
| 44 |
+
mime="text/html"
|
| 45 |
+
)
|
| 46 |
+
else:
|
| 47 |
+
st.error("No links found for the query.")
|
| 48 |
+
else:
|
| 49 |
+
st.error("Please enter a query.")
|
| 50 |
+
|
| 51 |
+
|