awacke1 commited on
Commit
aaf85bb
·
verified ·
1 Parent(s): 9416183

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -17
app.py CHANGED
@@ -1,23 +1,53 @@
 
1
  import streamlit as st
2
- from requests_html import HTMLSession
3
 
4
- def perform_search(search_query):
5
- session = HTMLSession()
6
- url = f'https://www.bing.com/search?q={search_query}'
7
- response = session.get(url)
8
- response.html.render(sleep=1)
9
- search_results = response.html.find('li.b_algo')
 
 
 
 
 
 
 
 
 
10
  results = []
11
  for result in search_results:
12
- title = result.find('h2', first=True).text
13
- link = result.find('a', first=True).attrs['href']
14
- results.append(f'{title}\n{link}')
 
15
  return results
16
 
17
- st.title('Web Automation with requests-html')
18
- search_query = st.text_input('Enter a search query')
19
- if st.button('Search'):
20
- results = perform_search(search_query)
21
- st.write('Search Results:')
22
- for result in results:
23
- st.write(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
 
28
+ def main():
29
+ st.set_page_config(page_title="Web Search App", page_icon=":mag:", layout="wide")
30
+
31
+ st.title("Web Search App")
32
+ st.write("Search Google and Bing simultaneously!")
33
+
34
+ search_query = st.text_input("Enter your search query")
35
+
36
+ col1, col2 = st.columns(2)
37
+
38
+ with col1:
39
+ st.header("Google Search Results")
40
+ if st.button("Search Google"):
41
+ google_results = asyncio.run(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 = asyncio.run(perform_search(search_query, "Bing"))
49
+ for result in bing_results:
50
+ st.write(f"[{result['title']}]({result['link']})")
51
+
52
+ if __name__ == "__main__":
53
+ main()