Makima57 commited on
Commit
12fdb18
·
verified ·
1 Parent(s): 898b622

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -1,34 +1,30 @@
1
 
2
  # app.py
3
  import streamlit as st
4
- from bs4 import BeautifulSoup
5
- import requests
6
- from urllib.parse import urljoin, urlparse
7
 
 
8
  def google_search(query):
9
- google_url = f"https://www.google.com/search?q={query}"
10
- response = requests.get(google_url, headers={"User-Agent": "Mozilla/5.0"})
11
- soup = BeautifulSoup(response.text, 'html.parser')
12
- links = soup.find_all('a')
13
- for link in links:
14
- href = link.get('href')
15
- if href and href.startswith('/url?q='):
16
- return href.split('&sa=U&ved=')[0].replace('/url?q=', '')
17
- return None
18
 
19
- def download_content(url):
20
- response = requests.get(url)
21
- return response.text
22
 
23
- st.title("Google Search")
24
- query = st.text_input("Enter your query")
 
 
25
  if st.button("Search"):
26
- link = google_search(query)
27
- if link:
28
- st.write("First result:", link)
29
- if st.button("Download content"):
30
- content = download_content(link)
31
- st.download_button("Download", content, file_name="content.html")
32
  else:
33
- st.write("No results found")
 
34
 
 
1
 
2
  # app.py
3
  import streamlit as st
4
+ from googlesearch import search
 
 
5
 
6
+ # Function to perform Google search and return the first link
7
  def google_search(query):
8
+ # Perform the search and get the first result
9
+ search_results = search(query, num=1, stop=1)
10
+ first_link = next(search_results, None)
11
+ return first_link
 
 
 
 
 
12
 
13
+ # Streamlit app UI
14
+ st.title("Google Search First Link Finder")
 
15
 
16
+ # Input field for search query
17
+ query = st.text_input("Enter search query", "")
18
+
19
+ # Button to trigger search
20
  if st.button("Search"):
21
+ if query:
22
+ first_link = google_search(query)
23
+ if first_link:
24
+ st.success(f"First link: [Click here]({first_link})")
25
+ else:
26
+ st.warning("No results found")
27
  else:
28
+ st.error("Please enter a query")
29
+
30