Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -83,33 +83,45 @@ def search_web(query):
|
|
83 |
|
84 |
# Scrape organic search results
|
85 |
result["data"]["organic"] = []
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
item = {"title": title, "url": url, "snippet": snippet}
|
91 |
result["data"]["organic"].append(item)
|
92 |
|
93 |
# Scrape knowledge panel
|
94 |
result["data"]["knowledge_panel"] = {}
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
98 |
|
99 |
# Scrape images
|
100 |
result["data"]["images"] = []
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
106 |
result["data"]["images"].append(item)
|
107 |
else:
|
108 |
result["error"] = "Failed to retrieve search results"
|
109 |
except Exception as e:
|
110 |
result["error"] = f"An error occurred: {e}"
|
111 |
return result
|
112 |
-
|
113 |
full_response = None # Initialize full_response to None
|
114 |
|
115 |
if prompt := st.chat_input("Enter your prompt here..."):
|
|
|
83 |
|
84 |
# Scrape organic search results
|
85 |
result["data"]["organic"] = []
|
86 |
+
results = soup.find_all('div', class_='g')
|
87 |
+
for result in results:
|
88 |
+
a_tag = result.find('a')
|
89 |
+
if a_tag is not None and 'title' in a_tag.attrs:
|
90 |
+
title = a_tag['title']
|
91 |
+
else:
|
92 |
+
title = ''
|
93 |
+
url = a_tag['href'] if 'href' in a_tag.attrs else ''
|
94 |
+
snippet = result.find('span', class_='aCOpRe')
|
95 |
+
if snippet is not None:
|
96 |
+
snippet = snippet.text
|
97 |
+
else:
|
98 |
+
snippet = ''
|
99 |
item = {"title": title, "url": url, "snippet": snippet}
|
100 |
result["data"]["organic"].append(item)
|
101 |
|
102 |
# Scrape knowledge panel
|
103 |
result["data"]["knowledge_panel"] = {}
|
104 |
+
knowledge_panel = soup.find('div', id='knowledge-kp')
|
105 |
+
if knowledge_panel is not None:
|
106 |
+
title = knowledge_panel.find('h3').text
|
107 |
+
content = knowledge_panel.find('div', class_='VwiC3b').text
|
108 |
+
result["data"]["knowledge_panel"]["title"] = title
|
109 |
+
result["data"]["knowledge_panel"]["content"] = content
|
110 |
|
111 |
# Scrape images
|
112 |
result["data"]["images"] = []
|
113 |
+
images = soup.find_all('div', class_='hdtb-mitem hdtb-msel')
|
114 |
+
for image in images:
|
115 |
+
a_tag = image.find('a')
|
116 |
+
title = a_tag['title'] if 'title' in a_tag.attrs else ''
|
117 |
+
url = a_tag['href'] if 'href' in a_tag.attrs else ''
|
118 |
+
item = {"title": title, "url": url, "snippet": ''}
|
119 |
result["data"]["images"].append(item)
|
120 |
else:
|
121 |
result["error"] = "Failed to retrieve search results"
|
122 |
except Exception as e:
|
123 |
result["error"] = f"An error occurred: {e}"
|
124 |
return result
|
|
|
125 |
full_response = None # Initialize full_response to None
|
126 |
|
127 |
if prompt := st.chat_input("Enter your prompt here..."):
|