Spaces:
Sleeping
Sleeping
import asyncio | |
import streamlit as st | |
from requests_html import AsyncHTMLSession | |
async def perform_search(search_query, search_engine): | |
session = AsyncHTMLSession() | |
if search_engine == "Google": | |
url = f"https://www.google.com/search?q={search_query}" | |
elif search_engine == "Bing": | |
url = f"https://www.bing.com/search?q={search_query}" | |
response = await session.get(url) | |
await response.html.arender(sleep=1) | |
# Extract search results based on the search engine | |
if search_engine == "Google": | |
search_results = response.html.find(".yuRUbf a") | |
elif search_engine == "Bing": | |
search_results = response.html.find(".b_algo h2 a") | |
results = [] | |
for result in search_results: | |
title = result.text | |
link = result.attrs["href"] | |
results.append({"title": title, "link": link}) | |
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 and Bing simultaneously!") | |
search_query = st.text_input("Enter your search query") | |
col1, col2 = st.columns(2) | |
with col1: | |
st.header("Google Search Results") | |
if st.button("Search Google"): | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
google_results = loop.run_until_complete(perform_search(search_query, "Google")) | |
for result in google_results: | |
st.write(f"[{result['title']}]({result['link']})") | |
with col2: | |
st.header("Bing Search Results") | |
if st.button("Search Bing"): | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
bing_results = loop.run_until_complete(perform_search(search_query, "Bing")) | |
for result in bing_results: | |
st.write(f"[{result['title']}]({result['link']})") | |
if __name__ == "__main__": | |
main() |