Makima57 commited on
Commit
1640937
1 Parent(s): 9c08365

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -1,27 +1,32 @@
1
 
 
2
  # app.py
3
  # app.py
4
  import streamlit as st
5
  import requests
6
  from bs4 import BeautifulSoup
7
- from serpapi import GoogleSearch
8
-
9
- SERP_API_KEY = "your_serpapi_key" # Replace with your SerpApi key
10
 
11
  def get_first_link(query):
12
  try:
13
- params = {
14
- "engine": "google",
15
- "q": query,
16
- "api_key": SERP_API_KEY,
17
  }
18
- search = GoogleSearch(params)
19
- results = search.get_dict()
20
- if "organic_results" in results and results["organic_results"]:
21
- first_link = results["organic_results"][0]["link"]
 
 
 
 
 
 
22
  return first_link
23
  else:
24
- st.error("No organic results found.")
25
  return None
26
  except Exception as e:
27
  st.error(f"Error fetching search results: {e}")
@@ -63,5 +68,4 @@ if st.button("Fetch First Link and Download Content"):
63
  st.error("Please enter a query.")
64
 
65
 
66
-
67
 
 
1
 
2
+ # app.py
3
  # app.py
4
  # app.py
5
  import streamlit as st
6
  import requests
7
  from bs4 import BeautifulSoup
8
+ import re
 
 
9
 
10
  def get_first_link(query):
11
  try:
12
+ # Use a basic Google search URL (may not work indefinitely)
13
+ url = f"https://www.google.com/search?q={query.replace(' ', '+')}"
14
+ headers = {
15
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
16
  }
17
+ response = requests.get(url, headers=headers)
18
+ response.raise_for_status()
19
+
20
+ # Parse the search results page
21
+ soup = BeautifulSoup(response.text, 'html.parser')
22
+
23
+ # Find the first link in the search results
24
+ link_elements = soup.find_all('a', href=re.compile(r"\/url\?q="))
25
+ if link_elements:
26
+ first_link = re.search(r'/url\?q=(.*?)&', link_elements[0]['href']).group(1)
27
  return first_link
28
  else:
29
+ st.error("No links found in the search results.")
30
  return None
31
  except Exception as e:
32
  st.error(f"Error fetching search results: {e}")
 
68
  st.error("Please enter a query.")
69
 
70
 
 
71