awacke1's picture
Create app.py
caa28a1 verified
import requests
from bs4 import BeautifulSoup
import streamlit as st
from urllib.parse import quote
@st.cache_resource
def display_glossary_entity(k):
search_urls = {
"🚀🌌ArXiv": lambda k: f"https://arxiv.org/search/?query={quote(k)}&searchtype=all&source=header",
"📖Wiki": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
"🔍Google": lambda k: f"https://www.google.com/search?q={quote(k)}",
"🔎Bing": lambda k: f"https://www.bing.com/search?q={quote(k)}",
"🎥YouTube": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
"🐦Twitter": lambda k: f"https://twitter.com/search?q={quote(k)}",
}
links_md = ' '.join([f"[{emoji}]({url(k)})" for emoji, url in search_urls.items()])
st.markdown(f"**{k}** <small>{links_md}</small>", unsafe_allow_html=True)
def perform_search(search_query, search_engine):
if search_engine == "Google":
url = f"https://www.google.com/search?q={search_query}"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
html_content = soup.prettify()
text_content = soup.get_text()
links = [link["href"] for link in soup.find_all("a", href=True)]
images = [img["src"] for img in soup.find_all("img", src=True)]
results = {
"html": html_content,
"text": text_content,
"links": links,
"images": images
}
elif search_engine == "Bing":
url = f"https://www.bing.com/search?q={search_query}"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
html_content = soup.prettify()
text_content = soup.get_text()
links = [link["href"] for link in soup.find_all("a", href=True)]
images = [img["src"] for img in soup.find_all("img", src=True)]
results = {
"html": html_content,
"text": text_content,
"links": links,
"images": images
}
elif search_engine == "Wikipedia":
url = f"https://en.wikipedia.org/w/index.php?search={search_query}"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
html_content = soup.prettify()
text_content = soup.get_text()
links = [link["href"] for link in soup.find_all("a", href=True)]
images = [img["src"] for img in soup.find_all("img", src=True)]
results = {
"html": html_content,
"text": text_content,
"links": links,
"images": images
}
elif search_engine == "Twitter":
url = f"https://twitter.com/search?q={search_query}"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
html_content = soup.prettify()
text_content = soup.get_text()
links = [link["href"] for link in soup.find_all("a", href=True)]
images = [img["src"] for img in soup.find_all("img", src=True)]
results = {
"html": html_content,
"text": text_content,
"links": links,
"images": images
}
return results
def main():
st.set_page_config(page_title="Web Search App", page_icon=":mag:", layout="wide")
st.title("Web Search App")
st.write("Search Google, Bing, Wikipedia, and Twitter simultaneously!")
search_query = st.text_input("Enter your search query")
display_glossary_entity(search_query)
col1, col2, col3, col4 = st.columns(4)
with col1:
st.header("Google Search Results")
if st.button("Search Google"):
google_results = perform_search(search_query, "Google")
st.write("HTML Content:")
st.code(google_results["html"], language="html")
st.write("Text Content:")
st.write(google_results["text"])
st.write("Links:")
for link in google_results["links"]:
st.write(link)
st.write("Images:")
for image in google_results["images"]:
st.image(image)
with col2:
st.header("Bing Search Results")
if st.button("Search Bing"):
bing_results = perform_search(search_query, "Bing")
st.write("HTML Content:")
st.code(bing_results["html"], language="html")
st.write("Text Content:")
st.write(bing_results["text"])
st.write("Links:")
for link in bing_results["links"]:
st.write(link)
st.write("Images:")
for image in bing_results["images"]:
st.image(image)
with col3:
st.header("Wikipedia Search Results")
if st.button("Search Wikipedia"):
wikipedia_results = perform_search(search_query, "Wikipedia")
st.write("HTML Content:")
st.code(wikipedia_results["html"], language="html")
st.write("Text Content:")
st.write(wikipedia_results["text"])
st.write("Links:")
for link in wikipedia_results["links"]:
st.write(link)
st.write("Images:")
for image in wikipedia_results["images"]:
st.image(image)
with col4:
st.header("Twitter Search Results")
if st.button("Search Twitter"):
twitter_results = perform_search(search_query, "Twitter")
st.write("HTML Content:")
st.code(twitter_results["html"], language="html")
st.write("Text Content:")
st.write(twitter_results["text"])
st.write("Links:")
for link in twitter_results["links"]:
st.write(link)
st.write("Images:")
for image in twitter_results["images"]:
st.image(image)
if __name__ == "__main__":
main()