awacke1 commited on
Commit
c2f0197
·
verified ·
1 Parent(s): 7e99736

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -1,26 +1,26 @@
1
- import asyncio
 
2
  import streamlit as st
3
- from requests_html import AsyncHTMLSession
4
 
5
- async def perform_search(search_query, search_engine):
6
- session = AsyncHTMLSession()
7
  if search_engine == "Google":
8
  url = f"https://www.google.com/search?q={search_query}"
9
  elif search_engine == "Bing":
10
  url = f"https://www.bing.com/search?q={search_query}"
11
- response = await session.get(url)
12
- await response.html.arender(sleep=1)
 
13
 
14
  # Extract search results based on the search engine
15
  if search_engine == "Google":
16
- search_results = response.html.find(".yuRUbf a")
17
  elif search_engine == "Bing":
18
- search_results = response.html.find(".b_algo h2 a")
19
 
20
  results = []
21
  for result in search_results:
22
  title = result.text
23
- link = result.attrs["href"]
24
  results.append({"title": title, "link": link})
25
 
26
  return results
@@ -38,18 +38,14 @@ def main():
38
  with col1:
39
  st.header("Google Search Results")
40
  if st.button("Search Google"):
41
- loop = asyncio.new_event_loop()
42
- asyncio.set_event_loop(loop)
43
- google_results = loop.run_until_complete(perform_search(search_query, "Google"))
44
  for result in google_results:
45
  st.write(f"[{result['title']}]({result['link']})")
46
 
47
  with col2:
48
  st.header("Bing Search Results")
49
  if st.button("Search Bing"):
50
- loop = asyncio.new_event_loop()
51
- asyncio.set_event_loop(loop)
52
- bing_results = loop.run_until_complete(perform_search(search_query, "Bing"))
53
  for result in bing_results:
54
  st.write(f"[{result['title']}]({result['link']})")
55
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
  import streamlit as st
 
4
 
5
+ def perform_search(search_query, search_engine):
 
6
  if search_engine == "Google":
7
  url = f"https://www.google.com/search?q={search_query}"
8
  elif search_engine == "Bing":
9
  url = f"https://www.bing.com/search?q={search_query}"
10
+
11
+ response = requests.get(url)
12
+ soup = BeautifulSoup(response.text, "html.parser")
13
 
14
  # Extract search results based on the search engine
15
  if search_engine == "Google":
16
+ search_results = soup.select(".yuRUbf a")
17
  elif search_engine == "Bing":
18
+ search_results = soup.select(".b_algo h2 a")
19
 
20
  results = []
21
  for result in search_results:
22
  title = result.text
23
+ link = result["href"]
24
  results.append({"title": title, "link": link})
25
 
26
  return results
 
38
  with col1:
39
  st.header("Google Search Results")
40
  if st.button("Search Google"):
41
+ google_results = perform_search(search_query, "Google")
 
 
42
  for result in google_results:
43
  st.write(f"[{result['title']}]({result['link']})")
44
 
45
  with col2:
46
  st.header("Bing Search Results")
47
  if st.button("Search Bing"):
48
+ bing_results = perform_search(search_query, "Bing")
 
 
49
  for result in bing_results:
50
  st.write(f"[{result['title']}]({result['link']})")
51