Makima57 commited on
Commit
9c08365
·
verified ·
1 Parent(s): 810c675

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -2,15 +2,27 @@
2
  # app.py
3
  # app.py
4
  import streamlit as st
5
- from googlesearch import search
6
  import requests
7
  from bs4 import BeautifulSoup
 
 
 
8
 
9
  def get_first_link(query):
10
  try:
11
- # Use the search function without 'num' argument
12
- for result in search(query, stop=1): # Remove 'num=1'
13
- return result
 
 
 
 
 
 
 
 
 
 
14
  except Exception as e:
15
  st.error(f"Error fetching search results: {e}")
16
  return None
@@ -51,4 +63,5 @@ if st.button("Fetch First Link and Download Content"):
51
  st.error("Please enter a query.")
52
 
53
 
 
54
 
 
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}")
28
  return None
 
63
  st.error("Please enter a query.")
64
 
65
 
66
+
67