Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -207,6 +207,7 @@ def generate_mock_search_results(query):
|
|
| 207 |
|
| 208 |
# Google ๊ฒ์ ํจ์ (SerpAPI ๋์ ์ง์ ๊ฒ์)
|
| 209 |
# Google ๊ฒ์ ํจ์ (BeautifulSoup์ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ ํ์ฑ)
|
|
|
|
| 210 |
def do_google_search(query, num_results=5):
|
| 211 |
try:
|
| 212 |
# ๋ค์ํ User-Agent ์ฌ์ฉ (Google ์ฐจ๋จ ๋ฐฉ์ง)
|
|
@@ -214,19 +215,14 @@ def do_google_search(query, num_results=5):
|
|
| 214 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 215 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 216 |
'Accept-Language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
|
| 217 |
-
'Accept-Encoding': 'gzip, deflate, br',
|
| 218 |
'Referer': 'https://www.google.com/',
|
| 219 |
-
'DNT': '1',
|
| 220 |
-
'Connection': 'keep-alive',
|
| 221 |
-
'Upgrade-Insecure-Requests': '1',
|
| 222 |
-
'Cache-Control': 'max-age=0',
|
| 223 |
}
|
| 224 |
|
| 225 |
-
# ๊ฒ์ URL
|
| 226 |
search_url = f"https://www.google.com/search?q={query}&num={num_results}&hl=ko&gl=kr"
|
| 227 |
logging.info(f"๊ตฌ๊ธ ๊ฒ์ URL: {search_url}")
|
| 228 |
|
| 229 |
-
# ์์ฒญ ๋ณด๋ด๊ธฐ
|
| 230 |
response = requests.get(search_url, headers=headers, timeout=10)
|
| 231 |
|
| 232 |
# ์๋ต์ด ์ฑ๊ณต์ ์ธ์ง ํ์ธ
|
|
@@ -234,62 +230,133 @@ def do_google_search(query, num_results=5):
|
|
| 234 |
logging.error(f"Google ๊ฒ์ ์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
| 235 |
return generate_mock_search_results(query)
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
# BeautifulSoup์ผ๋ก HTML ํ์ฑ
|
| 238 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 239 |
|
| 240 |
# ๊ฒ์ ๊ฒฐ๊ณผ ์ถ์ถ
|
| 241 |
organic_results = []
|
| 242 |
|
| 243 |
-
# ๊ฒ์ ๊ฒฐ๊ณผ
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
continue
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
if not organic_results:
|
| 292 |
-
logging.warning("๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ฑํ ์ ์์ต๋๋ค.
|
| 293 |
return generate_mock_search_results(query)
|
| 294 |
|
| 295 |
# ๊ฒ์ ๊ฒฐ๊ณผ ๋งํฌ๋ค์ด ํ์์ผ๋ก ๋ณํ
|
|
|
|
| 207 |
|
| 208 |
# Google ๊ฒ์ ํจ์ (SerpAPI ๋์ ์ง์ ๊ฒ์)
|
| 209 |
# Google ๊ฒ์ ํจ์ (BeautifulSoup์ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ ํ์ฑ)
|
| 210 |
+
# Google ๊ฒ์ ํจ์ ๊ฐ์
|
| 211 |
def do_google_search(query, num_results=5):
|
| 212 |
try:
|
| 213 |
# ๋ค์ํ User-Agent ์ฌ์ฉ (Google ์ฐจ๋จ ๋ฐฉ์ง)
|
|
|
|
| 215 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 216 |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
| 217 |
'Accept-Language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
|
|
|
|
| 218 |
'Referer': 'https://www.google.com/',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
}
|
| 220 |
|
| 221 |
+
# ๊ฒ์ URL
|
| 222 |
search_url = f"https://www.google.com/search?q={query}&num={num_results}&hl=ko&gl=kr"
|
| 223 |
logging.info(f"๊ตฌ๊ธ ๊ฒ์ URL: {search_url}")
|
| 224 |
|
| 225 |
+
# ์์ฒญ ๋ณด๋ด๊ธฐ
|
| 226 |
response = requests.get(search_url, headers=headers, timeout=10)
|
| 227 |
|
| 228 |
# ์๋ต์ด ์ฑ๊ณต์ ์ธ์ง ํ์ธ
|
|
|
|
| 230 |
logging.error(f"Google ๊ฒ์ ์๋ต ์ํ ์ฝ๋: {response.status_code}")
|
| 231 |
return generate_mock_search_results(query)
|
| 232 |
|
| 233 |
+
# HTML ํ์ฑ์ ์ํ ๋๋ฒ๊น
|
| 234 |
+
with open("google_response.html", "w", encoding="utf-8") as f:
|
| 235 |
+
f.write(response.text)
|
| 236 |
+
logging.info("Google ์๋ต HTML์ 'google_response.html'์ ์ ์ฅํ์ต๋๋ค.")
|
| 237 |
+
|
| 238 |
# BeautifulSoup์ผ๋ก HTML ํ์ฑ
|
| 239 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 240 |
|
| 241 |
# ๊ฒ์ ๊ฒฐ๊ณผ ์ถ์ถ
|
| 242 |
organic_results = []
|
| 243 |
|
| 244 |
+
# 2023-2025 ๊ตฌ๊ธ ๊ฒ์ ๊ฒฐ๊ณผ ์ ํ์ ์๋ (๋ค์ํ ์ ํ์ ์๋)
|
| 245 |
+
selectors = [
|
| 246 |
+
'div.g', 'div.Gx5Zad', 'div.tF2Cxc', 'div.yuRUbf',
|
| 247 |
+
'.v5yQqb', '.MjjYud', 'div[jscontroller]', '.fP1Qef',
|
| 248 |
+
'div[data-sokoban-container]', '.hlcw0c'
|
| 249 |
+
]
|
| 250 |
|
| 251 |
+
# ๋ชจ๋ ์ ํ์ ์๋
|
| 252 |
+
containers = []
|
| 253 |
+
for selector in selectors:
|
| 254 |
+
elements = soup.select(selector)
|
| 255 |
+
if elements:
|
| 256 |
+
logging.info(f"์ ํ์ '{selector}'๋ก {len(elements)}๊ฐ ์์๋ฅผ ์ฐพ์์ต๋๋ค.")
|
| 257 |
+
containers.extend(elements)
|
| 258 |
+
# ์ถฉ๋ถํ ์์๋ฅผ ์ฐพ์์ผ๋ฉด ์ค๋จ
|
| 259 |
+
if len(containers) >= num_results * 2: # ์ค๋ณต ๊ฐ๋ฅ์ฑ ๊ณ ๋ คํด 2๋ฐฐ๋ก ์ฐพ์
|
| 260 |
+
break
|
| 261 |
|
| 262 |
+
# ๋์: ๋ชจ๋ <a> ํ๊ทธ์์ href๊ฐ http๋ก ์์ํ๋ ๊ฒ๋ง ์ ํ
|
| 263 |
+
if not containers:
|
| 264 |
+
logging.warning("๊ตฌ์กฐํ๋ ์ปจํ
์ด๋๋ฅผ ์ฐพ์ ์ ์์ด ์ง์ ๋งํฌ๋ฅผ ๊ฒ์ํฉ๋๋ค.")
|
| 265 |
+
link_elements = soup.select('a[href^="http"]')
|
| 266 |
+
|
| 267 |
+
for link in link_elements:
|
| 268 |
+
if len(organic_results) >= num_results:
|
| 269 |
+
break
|
| 270 |
+
|
| 271 |
+
href = link.get('href', '')
|
| 272 |
+
# Google ๋ฆฌ๋ค์ด๋ ํธ URL ํํฐ๋ง
|
| 273 |
+
if '/url?' in href or 'google.com' in href:
|
| 274 |
+
continue
|
| 275 |
|
| 276 |
+
# ๋ถ๋ชจ ์์์์ ํ
์คํธ ์ถ์ถ ์๋
|
| 277 |
+
parent = link.parent
|
| 278 |
+
title = link.get_text(strip=True) or "์ ๋ชฉ ์์"
|
|
|
|
| 279 |
|
| 280 |
+
# ์ถฉ๋ถํ ๊ธธ์ด์ ํ
์คํธ๊ฐ ์๋ ๊ฒฝ์ฐ๋ง ๊ฒฐ๊ณผ๋ก ์ถ๊ฐ
|
| 281 |
+
if len(title) > 5: # ์๋ฏธ ์๋ ์ ๋ชฉ์ ๋ณดํต 5์ ์ด์
|
| 282 |
+
# ์ฃผ๋ณ ํ
์คํธ ์ถ์ถ ์๋
|
| 283 |
+
surrounding_text = ""
|
| 284 |
+
for sibling in parent.next_siblings:
|
| 285 |
+
if sibling.name and sibling.get_text(strip=True):
|
| 286 |
+
surrounding_text = sibling.get_text(strip=True)
|
| 287 |
+
break
|
| 288 |
+
|
| 289 |
+
# ๊ฒฐ๊ณผ ์ถ๊ฐ
|
| 290 |
+
organic_results.append({
|
| 291 |
+
"title": title,
|
| 292 |
+
"link": href,
|
| 293 |
+
"snippet": surrounding_text or "์ถ๊ฐ ์ค๋ช
์์",
|
| 294 |
+
"displayed_link": href.split('/')[2] if '/' in href else href
|
| 295 |
+
})
|
| 296 |
+
|
| 297 |
+
# ์ ํ์ ๊ธฐ๋ฐ ํ์ฑ ์๋
|
| 298 |
+
else:
|
| 299 |
+
for container in containers:
|
| 300 |
+
if len(organic_results) >= num_results:
|
| 301 |
+
break
|
| 302 |
|
| 303 |
+
# 1. ๋งํฌ ์ฐพ๊ธฐ
|
| 304 |
+
link_element = container.select_one('a[href^="http"]')
|
| 305 |
+
if not link_element:
|
| 306 |
+
continue
|
| 307 |
+
|
| 308 |
+
link = link_element.get('href', '')
|
| 309 |
+
# Google์ ๋ฆฌ๋ค์ด๋ ํธ URL์ด๋ฉด ๊ฑด๋๋ฐ๊ธฐ
|
| 310 |
+
if '/url?' in link or 'google.com' in link:
|
| 311 |
+
continue
|
| 312 |
+
|
| 313 |
+
# 2. ์ ๋ชฉ ์ฐพ๊ธฐ (๋ค์ํ ์ ํ์)
|
| 314 |
+
title_selectors = ['h3', '.LC20lb', '.DKV0Md', '.l', '.vvjwJb']
|
| 315 |
+
title = None
|
| 316 |
+
for selector in title_selectors:
|
| 317 |
+
title_element = container.select_one(selector)
|
| 318 |
+
if title_element and title_element.get_text(strip=True):
|
| 319 |
+
title = title_element.get_text(strip=True)
|
| 320 |
+
break
|
| 321 |
+
|
| 322 |
+
if not title:
|
| 323 |
+
title = link_element.get_text(strip=True) or "์ ๋ชฉ ์์"
|
| 324 |
+
|
| 325 |
+
# 3. ์ค๋ํซ ์ฐพ๊ธฐ (๋ค์ํ ์ ํ์)
|
| 326 |
+
snippet_selectors = ['.VwiC3b', '.lyLwlc', '.yXK7lf', '.lEBKkf', '.s', '.st']
|
| 327 |
+
snippet = "์ค๋ช
์์"
|
| 328 |
+
for selector in snippet_selectors:
|
| 329 |
+
snippet_element = container.select_one(selector)
|
| 330 |
+
if snippet_element and snippet_element.get_text(strip=True):
|
| 331 |
+
snippet = snippet_element.get_text(strip=True)
|
| 332 |
+
break
|
| 333 |
+
|
| 334 |
+
# 4. ํ์ ๋งํฌ ์ฐพ๊ธฐ
|
| 335 |
+
displayed_link_selectors = ['cite', '.UPmit', '.qLRx3b', '.iUh30']
|
| 336 |
+
displayed_link = link.split('/')[2] if '/' in link else link
|
| 337 |
+
for selector in displayed_link_selectors:
|
| 338 |
+
element = container.select_one(selector)
|
| 339 |
+
if element and element.get_text(strip=True):
|
| 340 |
+
displayed_link = element.get_text(strip=True)
|
| 341 |
+
break
|
| 342 |
+
|
| 343 |
+
# ์ค๋ณต ์ ๊ฑฐ
|
| 344 |
+
is_duplicate = False
|
| 345 |
+
for result in organic_results:
|
| 346 |
+
if result["link"] == link or result["title"] == title:
|
| 347 |
+
is_duplicate = True
|
| 348 |
+
break
|
| 349 |
+
|
| 350 |
+
if not is_duplicate:
|
| 351 |
+
organic_results.append({
|
| 352 |
+
"title": title,
|
| 353 |
+
"link": link,
|
| 354 |
+
"snippet": snippet,
|
| 355 |
+
"displayed_link": displayed_link
|
| 356 |
+
})
|
| 357 |
|
| 358 |
if not organic_results:
|
| 359 |
+
logging.warning("๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํ์ฑํ ์ ์์ต๋๋ค. ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํฉ๋๋ค.")
|
| 360 |
return generate_mock_search_results(query)
|
| 361 |
|
| 362 |
# ๊ฒ์ ๊ฒฐ๊ณผ ๋งํฌ๋ค์ด ํ์์ผ๋ก ๋ณํ
|