Makima57 commited on
Commit
80b7575
·
verified ·
1 Parent(s): 47813a1

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -5,28 +5,31 @@ from bs4 import BeautifulSoup
5
  import requests
6
  from urllib.parse import urljoin, urlparse
7
 
8
- def search(query, url):
9
- if not url.startswith("http"):
10
- url = "https://" + url
11
- parsed_url = urlparse(url)
12
- search_url = f"{parsed_url.scheme}://{parsed_url.netloc}/{query}/"
13
- return search_url
 
 
 
 
14
 
15
  def download_content(url):
16
  response = requests.get(url)
17
  return response.text
18
 
19
  st.title("Search and Download")
20
-
21
  query = st.text_input("Enter your query (e.g. beds)")
22
  url = st.text_input("Enter the URL to search from (e.g. https://www.daraz.com.np/)")
23
- search_button = st.button("Search")
24
-
25
- if search_button:
26
- link = search(query, url)
27
- st.write(f"First result: {link}")
28
- download_button = st.button("Download Content")
29
- if download_button:
30
- content = download_content(link)
31
- st.download_button("Download", content, file_name="content.html")
32
 
 
5
  import requests
6
  from urllib.parse import urljoin, urlparse
7
 
8
+ def google_search(query, url):
9
+ google_url = f"https://www.google.com/search?q={query}+site:{url}"
10
+ response = requests.get(google_url)
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("Search and Download")
 
24
  query = st.text_input("Enter your query (e.g. beds)")
25
  url = st.text_input("Enter the URL to search from (e.g. https://www.daraz.com.np/)")
26
+ if st.button("Search"):
27
+ link = google_search(query, url)
28
+ if link:
29
+ st.write("First result:", link)
30
+ if st.button("Download content"):
31
+ content = download_content(link)
32
+ st.download_button("Download", content, file_name="content.html")
33
+ else:
34
+ st.write("No results found")
35